ASP. NET dynamic load ascx user control

Source: Internet
Author: User
Tags tagname

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.