By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Dec 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

<je************@yahoo.se> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 17 '05 #2

P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!

<je************@yahoo.se> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

Dec 17 '05 #3

P: n/a
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.

<je************@yahoo.se> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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

Dec 17 '05 #4

P: n/a
Thanks for all answers!
Kevin i did it your way. Works great thanks !

// Jesper

Dec 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.