Groups | Blog | Home
all groups > asp.net building controls > february 2004 >

asp.net building controls : how to pass parameters when "invoking" a control


v1nce
2/24/2004 1:41:06 AM
Hi

I'm looking for the -correct- (if any) way to pass parameters when invoking a control
some kind o
loadControl(path_of_control,"param1='1' param2='help me'")

I really need this because we used a portal in which we can "plug" controls
I got some "generic controls" and I want to specialize them (no modification of cod
involved only passing of some parameters

Until now I do it that wa
create the generic control "generic.ascx
create specialized control "specific.ascx
in VS I drag'n drop generic.ascx onto specific.ascx and modify the "html"

<%@ Register TagPrefix="uc1" TagName="generic" Src="../generic.ascx" %><uc1:generic id="specific" runat="server" param1="1" param2="elp me"></uc1:generic

This is really dumb (memory usage,end users can't pass parameters so I need to ad
controls at hand,recompilation needed everytime I add a control) so I'm looking for a solution

Can anyone point me to any resources or samples (especially in C#)?

thanks

tklawsuc NO[at]SPAM hotmail.com
2/24/2004 10:49:11 AM
[quoted text, click to view]

Not sure if this is the answer you are looking for but I recently used
the following method of creating user controls programatically and
passing/retrieving parameters to/from an aspx page.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconinstantiatingusercontrolsprogrammatically.asp

v1nce
2/26/2004 8:16:09 AM
Thanks Tom, that's exactly what I found from another source
You don't need to pass parameters when "invoking" the control. You just have to cast the control to the correct type and then you can access the properties

Control c1 = LoadControl("genericControl.ascx")
// by casting the control as a genericControl we can access his propertie
// it's that simple (*
((genericControl)c1).param1 = "1";
Page.Controls.Add(c1); // add the control to the Pag

(*) It's not that simple for me because I loop over n controls whose type is unknown to me
So I'll have to use some reflexion (GetType() GetProperty()
AddThis Social Bookmark Button