Casting a Custom Control

B

Bill

Hello All,

I have created a custom control that is supposed to add other custom
controls on the fly to a web page. I understand the concept of using
"controls.add" to cast that control to a page. The challenge here the
'main' control is the equivelent of a placeholder, so it will have no idea
what to display until it receives a text string (could come from different
sources).

So, a command like this would not work: Controls.Add("MyCustomControl").
This is because Controls.Add will not accept a string value. I also tried
converting the string to an object, but that failed as well (which I kind of
expected).

Could someone point me to how I can add a custom control to a page when the
control name is of a type string?

Thanks!

-Bill
 
C

Coskun SUNALI [MVP]

Hi,

You can get the Type using Type.GetType("MyCustomControl") then you have to
create a new instance of your object using this type.

Please feel free to ask if you need more assistance in advance.

--
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
http://www.propeople.dk
 
B

Bill

Hi Coskun,

Thank you very much for the support on this! I struggled with creating the
control and learned I need to add the namespace the custom control is
located in and use the activator to instantiate the control.

There is one more problem though... the Visual Studio designer (using 2008,
btw) is not aware of the control I am creating so I am not getting
type-ahead. The solution will not build either when I type to type the
property name by hand anyway. Here is the code I wrote:

Type myControlType =
Type.GetType("Namespace.CustomControl");
Control myControl =
(Control)Activator.CreateInstance(myControlType);
myControl.MyProperty = "hello world"; //This does not work.
MyProperty is never available in the designer.
this.Controls.Add(myControl);

Since the MyProperty (custom property in the custom control) is not exposed,
it would seem I cannot use this approach and would really appreciate your
support. Just so you know, I am aware the code is good because if I comment
out the Property line, my control does display on the page.

Thanks again for your help!

-Bill
 
C

Coskun SUNALI [MVP]

Hi Bill,

There are 2 ways of achieving what you want to do.

First, you may use Interfaces, if all your user controls will have similar
purposes. I mean, if all your custom controls will have "MyCustomProperty"
property. Then you can add a reference to this interface in your custom
controls' projects and your web project.

And you can have your object instance like that:

IMyControlType myControl =
(IMyControlType)Activator.CreateInstance(myControlType);
myControl.MyCustomProperty = "xyz";

Secondly, you may use reflection to set property values of your custom
control. If it sounds better to you than creating interfaces, I will suggest
you reading about Reflection and especially FieldInfo class.

Hope this helps.

--
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
http://www.propeople.dk
 
C

Coskun SUNALI [MVP]

Also,

If your custom control is located in a different project/assembly, you may
need to state the assembly name as well.

Type myControlType = Type.GetType("Namespace.CustomControl, AssemblyName");

Imagine that the assembly name is "Your.Controls", the following will work;

Type myControlType = Type.GetType("Namespace.CustomControl, Your.Controls");


--
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
http://www.propeople.dk
 
C

Coskun SUNALI [MVP]

Hi Bill,

You are welcome.

--
All the best,
Coskun SUNALI
MVP ASP/ASP.NET
http://sunali.com
http://www.propeople.dk

Hi Coskun,

Thank you for this information. I will look further into Interfaces from
what I can see, that would change an overall design so I'll put that one on
the back burner for a bit.

Here is the final Code I came up with:

Line 1: Type myControlsType =
Type.GetType("My.Controls.Namespace.CustomControl");
Line 2: WebControl myControl =
(WebControl)Activator.CreateInstance(myControlsType);
Line 3: myControl.GetType().InvokeMember("PropertyToSet",
BindingFlags.SetProperty, null, myControl, new object[]{ "Hello World!" });
Line 4: this.Controls.Add(myControl);

Thanks!

-Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top