Home All Groups Group Topic Archive Search About

Dynamically Loading user control

Author
14 Jan 2006 10:07 AM
Beegee
Hi
Happy 2006

I want to load the user control dynamically. I want to change to different
usercontrol page to the same place holder accordingly.

If there is no way to do so anyone please suggest me how to evaluate a
string of expression.
I have used the same in VBscript and javascript.
e.g.

stringvariable1 = "TemplateTopUserControl";
stringvariable2 = "\"TemplateTop.ascx\"";

"this." + stringvariable1 + " = (" + stringvariable1 + ")this.LoadControl("
+ stringvariable2 +")"

I want this to be executed as

this.TemplateTopUserControl  = (TemplateTopUserControl)
this.LoadControl("TemplateTop.ascx");

I know that we can use eval function available in javascript so I want to
know the equivalent in C#.


Thanks very much

BG

Author
14 Jan 2006 3:14 PM
Peter Bromberg [C# MVP]
Beegee,
..NET does not have the equivalent of an "eval" keyword as it is a compiled,
not an interpreted language / runtime.

If you want to provide something similar to scripting capabilities in your
app, you can compile your code on the fly into an in-memory assembly.

Another option would be to use either an interface, or a static Hashtable of
the keys / names of the controls to load, and have a switch or a set of if
-else statements to determine what to pass to the LoadControl method.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Show quoteHide quote
"Beegee" wrote:

> Hi
> Happy 2006
>
> I want to load the user control dynamically. I want to change to different
> usercontrol page to the same place holder accordingly.
>
> If there is no way to do so anyone please suggest me how to evaluate a
> string of expression.
> I have used the same in VBscript and javascript.
> e.g.
>
> stringvariable1 = "TemplateTopUserControl";
> stringvariable2 = "\"TemplateTop.ascx\"";
>
> "this." + stringvariable1 + " = (" + stringvariable1 + ")this.LoadControl("
> + stringvariable2 +")"
>
> I want this to be executed as
>
> this.TemplateTopUserControl  = (TemplateTopUserControl)
> this.LoadControl("TemplateTop.ascx");
>
> I know that we can use eval function available in javascript so I want to
> know the equivalent in C#.
>
>
> Thanks very much
>
> BG
>
>
>
>

Bookmark and Share