【问题标题】:How to load web user control dynamically?如何动态加载 Web 用户控件?
【发布时间】:2023-03-09 09:52:01
【问题描述】:

这是一个非常重要的问题。这让我在 4 小时内发疯 :( 我可以加载 UCAddX.ascx 但如果我单击“在 X 中搜索”按钮不会加载 UCSearchX 用户控件。有 3 个按钮也有 3 个 web 用户控件。我想加载这 3 个 web clickEvents 之后的用户控件。但是下面的方法不起作用。如何动态加载 Web 用户控件?单击单击(像 Tab 控件)

 public partial class MyPage: System.Web.UI.Page
    {
   protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["controlType"] = "AddX";
            if (!IsPostBack)
            {
                AddUserControl();
            }
            else
            {
                AddUserControl();
            }
        }

        protected void btnAddX_Click(object sender, DirectEventArgs e)
        {
            ViewState["controlType"] = "AddX";
            if (!IsPostBack)
                  AddUserControl();
            else
                AddUserControl();
        }

        protected void btnSearchX_Click(object sender, DirectEventArgs e)
        {
            ViewState["controlType"] = "SearchX";
            if (!IsPostBack)
                AddUserControl();
            else
                AddUserControl();

        }
        protected void btnUpdateX_Click(object sender, DirectEventArgs e)
        {

        }

        void AddUserControl()
        {
          //  plhContent1.Controls.Clear();
            if (ViewState["controlType"] != null)
            {
                if (ViewState["controlType"].ToString() == "AddX")
                {
                    UCAddX uc = (UCAddX)Page.LoadControl("~/Pages/EN/MyUserControls/UCAddX.ascx");
                    uc.ID = "ucAddX";
                    uc.Attributes.Add("runat", "Server");
                    uc.EnableViewState = true;
                    uc.Visible = true;
                    plhContent1.Controls.Add(uc);
                }
                else if (ViewState["controlType"].ToString() == "SearchX")
                {
                    UCSearchX uc = (UCSearchX)Page.LoadControl("~/Pages/EN/MyUserControls/UCSearchX.ascx");
                    uc.ID = "ucSearchX";
                    uc.Attributes.Add("runat", "Server");
                    uc.EnableViewState = true;
                    uc.Visible = true;
                    plhContent1.Controls.Add(uc);
                }


            }
        }

    }

【问题讨论】:

  • 在你的按钮点击事件中,我觉得修改ViewState已经来不及了。您可能需要 Session 状态。
  • 首先,我认为 4 小时对于解决一个重要问题或发疯或发疯来说并不算多。我记得有几个问题花了我几个星期才解决。其次,而不是动态加载这些用户控件我宁愿将它们放在不同的 ASP.NET 的 Panel 控件中并设置它们的 Visibility 属性,我不确定这是否是最好的方法。

标签: c# asp.net .net visual-studio-2010


【解决方案1】:

使用下面的代码动态加载用户控件

var control = LoadControl(filePath) as ControlType;

然后您可以订阅事件并添加到控件占位符。

希望对你有帮助

【讨论】:

  • @Penguen 为什么将信息存储在 ViewState 中而不是将其传递给 add 控件方法? void AddUserControl(Type controlType)
  • @Penguen 顺便说一句。您在 pageLoad 和按钮单击上调用 AddUserControl 方法...这是否意味着您正在尝试添加 2 个具有相同 ID 的控件???
【解决方案2】:

试试这样的,

//to load the control
     protected void Page_Init(object sender, EventArgs e)
     {
         ViewState["controlType"] = "AddSector";
         //you don't need to check the if condiontion, cause you load every time the controls
         AddUserControl();
      }

当您需要在回发后从控件中获取值时,您应该打开它

protected void Page_PreRender(object sender, EventsArgs args) {
    //your placeholder that contains data
}

【讨论】:

  • 我不明白 protected void Page_PreRender(object sender, EventsArgs args) { //你包含数据的占位符 }
  • 这里你得到了控件中存在的控件中包含的数据,因为预渲染事件几乎是最后发生的,你将无法看到之前的值。
【解决方案3】:

动态加载时的用户控件需要在每个 Page_Load 上加载,以保持其视图状态。所以你需要这样的东西:

    public string CurrentControlToLoad
    {
        get
        {
             if(ViewState["controlType"] == null)
                 return "";
             return (string)ViewState["controlType"];
        }
        set
        {
             ViewState["controlType"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if(CurrentControlToLoad != "")
           LoadControl(CurrentControlToLoad);
    }

    protected void btnAddSector_Click(object sender, DirectEventArgs e)
    {
        CurrentControlToLoad = "AddSector";
        LoadControl(CurrentControlToLoad);
    }

【讨论】:

    猜你喜欢
    • 2010-12-18
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多