Custom User Controls can be directly introduced on the page and dynamically loaded for use:
1. You can directly reference it in the aspx file on the page, and call its public attributes and methods through the control ID in its. CS file:
Eg:
Custom Control ityouhuictl:
Ityouhuictl. ascx. CS:
Define attributes:
Private string URL; Public String URL {get {return URL;} set {url = value ;}} Public String seturl (string urlinput, string para2) // set multiple variables through function call {url = urlinput ;...}
Page default. aspx:
<% @ Register src = "/controls/ityouhuictl. ascx "tagname =" ityouhuictl "tagprefix =" WFO "%> <WFO: ityouhuictlid =" YH "runat =" server "/>
Default. aspx. CS:
YH. url = "http://www.ityouhui.com"; // reference directly
2. The function call method is introduced by dynamically loading custom controls:
For function calls:
Eg:
Default. aspx:
<Asp: Panel id = "extpl1" runat = "server" visible = "false"/>
Daefault. aspx. CS:
Control C = page. loadcontrol (request. applicationpath + "/controls/ityouhuictl. ascx "); // dynamically load the Control Type TC = C. getType (); system. reflection. methodinfo M = tc. getmethod ("seturl"); // XX is the control's function object [] bjparas = new object [2]; bjparas [0] = "http://www.ityouhui.com/channel/notebook?#//the parameters bjparas [1] =" seller "; M. invoke (C, bjparas); // call extpl1.controls. add (c); // Add the control to the Panel placeholder
3. dynamically load custom controls and call properties:
First, declare the class name in the custom control:
Eg:
You need to add the classname attribute to the custom control ityouhuictl:
Ityouhuictl. ascx:
<% @ Control Language = "C #" autoeventwireup = "true" codefile = "ityouhuictl. ascx. cs"Classname= "Ityouhuictl" inherits = "controls_ityouhuictl" %>
Register in default. aspx on the reference page:
<% @ Register src = "/controls/ityouhuictl. ascx" tagname = "ityouhuictl" tagprefix = "WFO" %>
In default. aspx. CS, you can directly use:
ASP. ityouhuictl C; C = (Asp. ityouhuictl) (page. loadcontrol (request. applicationpath + "/controls/ityouhuictl. ascx "); extpl1.controls. add (c); C. url = "http://www.ityouhui.com/channel/camera?#//direct call! // Similarly, functions can be called here, which is simpler
Note: Before ityouhuictl, it is usually ASP. of course, different configurations may vary. If you do not know, you can directly enter the control name ityouhuictl at the call after completing the previous steps and move the mouse up, vs will prompt you to add a namespace and add it;
Over!
Org: http://blog.donews.com/me1105/archive/2011/05/15/154.aspx