Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > (Hard one,any expert out threre ? ) Create object from string ? add webcontrols dynamically on page.

Reply
Thread Tools

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

 
 
jesper_lofgren@yahoo.se
Guest
Posts: n/a
 
      12-17-2005
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

 
Reply With Quote
 
 
 
 
Mike Gaab
Guest
Posts: n/a
 
      12-17-2005

<> wrote in message
news: 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 =----
 
Reply With Quote
 
 
 
 
Karl Seguin
Guest
Posts: n/a
 
      12-17-2005
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!



<> wrote in message
news: 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
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      12-17-2005
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.

<> wrote in message
news: 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
>



 
Reply With Quote
 
jesper_lofgren@yahoo.se
Guest
Posts: n/a
 
      12-17-2005
Thanks for all answers!
Kevin i did it your way. Works great thanks !

// Jesper

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object reference not set to an instance of an object. System.Web.UI.WebControls.ListItemCollection.SaveViewState() System.Web.UI.WebControls.ListControl.SaveViewState() Brano ASP .Net 3 11-08-2005 12:29 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
bind values of dynamically loaded webcontrols to custom object ronnel.santiago ASP .Net Web Controls 0 10-04-2004 05:24 PM
create programatically webcontrols - dynamically tommy ASP .Net Web Controls 0 08-03-2004 03:27 PM
how to dynamically create a function object (from a code object)? Petri Savolainen Python 0 07-01-2003 10:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57