(Hard one,any expert out threre ? ) Create object from string ? add webcontrols dynamically on page.

J

jesper_lofgren

Hi there,

I have some webcontrols that i want to add dynamically on a page. I
have stored the path / namespace in database (ex
MyNameSpace.WebControls.Control1) to the class/webcontrol.

Lets say i have a column in database that looks like this.

PageID (int) Webcontrol (varchar)
1 MyNameSpace.WebControls.Control1
1 MyNameSpace.WebControls.Control2

Then i want to add the controls dynamically on the page, anyone know
how to do this ?
I guess i have to create a instance of the webcontrol and the add it
with Controls.add() . But how do i create the instance and add it
dynamically from a string?

Hope anyone can help me with this, some expert out there ?? :)

Thanks
Jesper
 
M

Mike Gaab

Hi there,

I have some webcontrols that i want to add dynamically on a page. I
have stored the path / namespace in database (ex
MyNameSpace.WebControls.Control1) to the class/webcontrol.

Lets say i have a column in database that looks like this.

PageID (int) Webcontrol (varchar)
1 MyNameSpace.WebControls.Control1
1 MyNameSpace.WebControls.Control2

Then i want to add the controls dynamically on the page, anyone know
how to do this ?
I guess i have to create a instance of the webcontrol and the add it
with Controls.add() . But how do i create the instance and add it
dynamically from a string?

Hope anyone can help me with this, some expert out there ?? :)

I am not an expert, just beginning but it you might consider placing
the controls on the webform in design mode with the visible property
set to false, then at runtime simple set the visible property to true as
needed.

Mike
 
K

Karl Seguin

It's much easier if you have the path. Controls are templated and aren't
instantiated like normal classes.

Control c = Page.LoadControl("~/controls/login.ascx");
container.Controls.Add(c);

Karl
 
K

Kevin Spencer

Assuming that there is some good reason for using a database to dynamically
load controls, I think you've gotten off on the wrong foot. Why are you
storing strings in the database? The names of namespaces and classes is
useful to us humans, but not particularly to computers. It would be more
efficient to store integers in the database. Assuming (again) that you know
what you're storing in the database, you could create Enumerations that
represent different namespaces and classes, and store the enumerated values
in the database. From there it's a simple matter of identifying the Control
to load via its enumerated value.

You would need some sort of mechanism to identify the Controls whose names
are stored in the database anyway. You could do it with Reflection, but that
would be expensive, and I see no benefit gained from it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
J

jesper_lofgren

Thanks for all answers!
Kevin i did it your way. Works great thanks !

// Jesper
 

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

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top