Original: ASP. NET dynamic loading of the Ascx user control
In the main aspx/ascx file, drag the target ASCX1,ASCX2 control to its page and delete it to generate: Register code, then define the DIV or TD in the main file, set the ID, runat= "server", Next LoadControl ("Ascx1")
<%@ Control language= "C #" autoeventwireup= "true" codefile= "LoginState.ascx.cs" inherits= "V3_loginstate"%>
<%@ Register src= "Loginstate1.ascx" tagname= "loginState1" tagprefix= "uc1"%>
<%@ Register src= "Loginstate2.ascx" tagname= "LoginState2" tagprefix= "UC2"%>
<%@ Register src= "Loginstate3.ascx" tagname= "LoginState3" tagprefix= "UC3"%>
<link href= ". /v3/css.css "rel=" stylesheet "type=" Text/css "/>
<div id= "logstate" runat= "Server" >
</div>
----------------
Whether to log in
if (session["vipusername"] = = null)
{
Not logged in
ct = LoadControl ("Loginstate1.ascx");
}
Else
{}
LogState.Controls.Clear ();
LOGSTATE.CONTROLS.ADD (CT);
----------------------
Other articles on the Web:
~ Dynamic Plus ascx~
. aspx
Example: Define a <div id= "test" runat= "Server" >
. aspx.cs
Control UC = Page.LoadControl ("Your. ascx path");
Test. Controls.Add (UC);
You can then display the contents of the control's UC. The control to invoke is registered to this page
More
Private Createcontorlui singleuicontrol;//Declaration user Control
private void Controladd ()
{
Singleuicontrol = (Createcontorlui) page.loadcontrol ("Createsingleui.ascx");//load user controls, where user controls and pages are in the same directory
Singleuicontrol.id = "Singelui";//Set User control ID
This. PLACEHOLDERSINGLE.CONTROLS.ADD (Singleuicontrol);//Add User control to page
}
--------------
Step One: Drag the ascx control into the design window and then into HTML mode, leaving the definition
if you need more than one ascx, please drag it all in.
but delete the corresponding content. (because we need to load dynamically)
Place a container, such as TD, in the area where the control needs to be filled, and set to run on the server side
such as
<td id= "Tdpan" RUNAT=SERVER></TD>
2: Dynamic Call
UserControl MyUserControl = (UserControl) LoadControl (".. /includes/pagenavigater.ascx ");
Type myusertype = MyUserControl. GetType ();
//The following is an assignment to the ascx
PropertyInfo myuserinfo1 = Myusertype. GetProperty ("Relateddatagrid");//). GetProperty ("Relateddatagrid");
Myuserinfo1. SetValue (myusercontrol,gridhwcy, NULL);
PropertyInfo mypassinfo = Myusertype. GetProperty ("Torefresh");
Mypassinfo. SetValue (Myusercontrol,true,null);
PropertyInfo myuserdatasource = Myusertype. GetProperty ("Relateddatasource");
Myuserdatasource. SetValue (Myusercontrol,dv,null);
Tdpan. Controls.clear ();
Tdpan. Controls.Add (MyUserControl);