简体   繁体   English

在aspx页面中动态加载用户控件

[英]dynamically load a user control in the aspx page

I have the following aspx page for eg: called choosemenu.aspx 我有以下aspx页面,例如:名为choosemenu.aspx

      <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">
 <head runat="server">
     <title></title>
 </head>
 <body>
     <form id="form1" runat="server">
         <div>
         </div>
       <div id="renderhere" runat="server">render user control here </div>
     </form>
 </body>
 </html>

I have a list of ascx pages called 我有一个称为的ascx页面列表

 english.ascx
 commerce.ascx
 maths.ascx

I have to dynamically load the ascx files in my aspx page depending on the querystring in the aspx page. 我必须根据aspx页中的querystring动态加载aspx页中的ascx文件。

I have the following contents in my aspx page in page_load event. 我在page_load事件的aspx页面中具有以下内容。

   var control = (English)Page.LoadControl("/ascx/english.ascx");

How will I render the contents of the english.ascx page in the choosemenu.aspx that too in this tag 我将如何在choicemenu.aspx中呈现english.ascx页面的内容,该内容也在此标记中

Also I have to pass some value in the ascx file. 另外,我还必须在ascx文件中传递一些值。 This is the static stuff. 这是静态的东西。

   <Menu:MNU ID="english" runat="server" HiLiter="<%#h %>"></Menu:MNU>

Loading a control from the server side 从服务器端加载控件

protected void Page_Load(object sender, EventArgs e)
{
    Page.Controls.Add(Page.LoadControl("~/ascx/english.ascx")); //CHECK THE PATH 
}

Loading a control from the server side and rendering it into a div If you want to render it in a specific div you might write: 从服务器端加载控件并将其呈现到div中如果要在特定的div呈现它,则可以编写:

protected void Page_Load(object sender, EventArgs e)
{
    UserControl uc = (UserControl)Page.LoadControl("~/ascx/english.ascx");
    uc.MyParameter = 1;
    uc.Id = 2;
    uc.someMethodToInitialize();
    div1.Controls.Add(uc);
}

and in your aspx page: 并在您的aspx页面中:

<div id="div1" runat="server">

</div>

Loading a control from the server side initializing the control with parameters 从服务器端加载控件,并使用参数初始化控件

If your control has a constructor with parameters , you have to use: 如果control具有带parametersconstructor parameters ,则必须使用:

public English_Control(int MyParameter, int Id) { //code here.. } public English_Control(int MyParameter,int Id){//在这里编码。

In you aspx.cs file you can initialize with: aspx.cs文件中,您可以使用以下命令进行初始化:

UserControl uc = (UserControl)Page.LoadControl(typeof(English_Control), new object[] {1, 2});
div1.Controls.Add(uc);

In order for the control's postback values to be available, you must load and reload it no later than PreInit. 为了使控件的回发值可用,必须不迟于PreInit加载和重新加载它。 Here is the code you need to do that. 这是您需要执行的代码。

    protected override void OnPreInit(EventArgs e)
    {
        string controlToLoad = String.Empty;
        //logic to determine which control to load
        UserControl userControl = (UserControl)LoadControl(controlToLoad);
        renderhere.Controls.Add(userControl);
        base.OnPreInit(e);
    }

As per MSDN : 根据MSDN

Pre-Init event used to "Create or re-create dynamic controls." 用于“创建或重新创建动态控件”的Pre-Init事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM