ASP.NET 动态添加自定义用户控件 [英] ASP.NET Custom user control to add dynamically

查看:31
本文介绍了ASP.NET 动态添加自定义用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将具有自定义用户控件的页面直接修改为 ASPX 页面,现在只需要在需要时动态加载它.用户控件确实通过 ASCX 文件包含 html 和其他控件,并在代码隐藏中包含代码.

我阅读了多个页面,发现无法直接实例化用户控件,而应使用 Page.LoadControl(...).问题不在于编译,而在于页面加载控件时,ASCX 中的所有控件都为空,然后崩溃.

如何使用在 ASCX 和代码隐藏中具有动态代码的用户控件?

我在做什么的例子(PageLoad 或 PagePreRender 或 PagePreInit)

 Control c = LoadControl(typeof(MyControl), null);myControl= (MyControl)c;myControl.ID = "123";myControl.Visible = false;Controls.Add(myControl);

MyControl 确实有例如

... 并且在 MyControl 中,它将可见性设置为 True 或 False...但是当它这样做时,现在它崩溃了,因为无论如何"div 为 NULL.

解决方案

我所做的是使用Page_Init中的Page.LoadControl方法添加自定义用户控制到页面上的占位符.

protected void Page_Init(object sender, EventArgs e){//MyControl 是带有代码隐藏文件的自定义用户控件MyControl myControl = (MyControl)Page.LoadControl("~/MyControl.ascx");//UserControlHolder是aspx页面上的占位符//我要将用户控件加载到的位置UserControlHolder.Controls.Add(myControl);}

这对我来说很好用.

这是动态加载的用户控件的代码:

MyControl.ascx.cs

公共部分类 MyControl : System.Web.UI.UserControl{protected void Page_Init(对象发送者,EventArgs e){LiteralControl lit = new LiteralControl("Test Literal Control");Page.Controls.Add(lit);}protected void Page_Load(object sender, EventArgs e){无论什么.可见 = 真;如果 (IsPostBack){不管什么.可见 = 假;}}}

I have hard time to modify a page that had a Custom User Control directly to the ASPX page and now require to have it dynamically loaded when needed only. The User Control does have html and other controls via the ASCX file and has code in the code-behind.

I have read multiple page and have found that I cannot instantiate directly the User Control but should use the Page.LoadControl(...). The problem is not the compiling but when the page load the control it happen that all controls inside the ASCX are null and then crash.

How can I use a User Control that has code in the ASCX and in the code-behind dynamically?

Edit:

Example of what I am doing in (PageLoad or PagePreRender or PagePreInit)

      Control c = LoadControl(typeof(MyControl), null);
      myControl= (MyControl)c;
      myControl.ID = "123";
      myControl.Visible = false;
      Controls.Add(myControl);

MyControl does have for example <div id="whatever" runat="server">... and inside the MyControl it set the visibility to True or False... but when it does that, now it crash because the "whatever" div is NULL.

解决方案

What I have done is use the Page.LoadControl method in the Page_Init to add the custom user control to a place holder on the page.

protected void Page_Init(object sender, EventArgs e)
{
    //MyControl is the Custom User Control with a code behind file
    MyControl myControl = (MyControl)Page.LoadControl("~/MyControl.ascx");

    //UserControlHolder is a place holder on the aspx page
    // where I want to load the user control to
    UserControlHolder.Controls.Add(myControl);
}

This works fine for me.

Here is the code for the dynamically loaded user control:

MyControl.ascx.cs

public partial class MyControl : System.Web.UI.UserControl
{
    protected void Page_Init(object sender, EventArgs e)
    {
        LiteralControl lit = new LiteralControl("Test Literal Control");
        Page.Controls.Add(lit);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        whatever.Visible = true;

        if (IsPostBack)
        {
            whatever.Visible = false;
        }
    }
}

这篇关于ASP.NET 动态添加自定义用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆