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

  • Thread starter Thread starter jesper_lofgren
  • Start date Start date
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
 
<[email protected]> a écrit dans le message de (e-mail address removed)...

| 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?

Assuming the types are declared in the same assembly, then you can use this
version of Activator :

Control newControl = (Control) Activator.CreateInstance(null,
"MyNameSpace.WebControls.Control1");

If the type is in another assembly, then the first parameter should be the
assembly name.

Joanna
 

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

Back
Top