【问题标题】:Dynamically loaded web user control hides on postback动态加载的 Web 用户控件在回发时隐藏
【发布时间】:2011-11-30 09:07:22
【问题描述】:

我有一个网络自定义控件

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
    Inherits="WebApplication5.WebUserControl1" %>
<asp:DropDownList ID="ddlnew" AutoPostBack="true" runat="server" 
    onselectedindexchanged="ddlnew_SelectedIndexChanged">
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text2" />
    <asp:ListItem Text="text1" />
    <asp:ListItem Text="text2" />
    <asp:ListItem Text="text2" />
</asp:DropDownList>

在 Default.aspx 页面上

<asp:Button Text="PostBack" ID="btnPost" runat="server" 
onclick="btnPost_Click" />

在 Default.aspx.cs 上

    protected void btnPost_Click(object sender, EventArgs e)
    {
        WebUserControl1 uc =  (WebUserControl1)Page.LoadControl("~/WebUserControl1.ascx");
        PlaceHolder.Controls.Add(uc);
    }

当我们从下拉列表中选择任何项目时,它会回发并且控件从页面中隐藏

所以请帮助如何防止隐藏

谢谢

【问题讨论】:

    标签: c# asp.net user-controls updatepanel


    【解决方案1】:

    动态创建控件must be recreated on every postback,当您意识到每次回发都会创建Page 类的新实例,并且在此实例中您必须每次都重新创建所有控件时,这一点变得更加明显。

    Here is a good article on this

    Another post on this that i answered

    And another

    要保持回发之间的状态,您可以使用ViewState 或ControlState

    MSDN Control State vs View State Example

    【讨论】:

    【解决方案2】:
    protected void ddlnew_SelectedIndexChanged(object o, EventArgs e)
    {
      ViewState["ddlnew_value"]=ddlnew.selectdeitem; 
    }
    

    比在页面加载pu这个

    If(IsPostBack)
    {
        if(ViewState["ddlnew_value"]!=null)
        {
            ddlnew.selecteditem=ViewState["ddlnew_value"];
        }
     }
    

    这应该可以工作

    【讨论】:

      【解决方案3】:

      在 ASP.NET 中,动态加载的控件需要一些注意,因为它们在回发中的行为。您必须在回发或check that on which control's is generating postback you will load the control or not..

      中维护动态控件的视图状态

      查看这些文章(特别是 MSDN 参考):
      An Extensive Examination of User Controls - MSDN
      Loading UserControl Dynamically in UpdatePanel
      Maintain Viewstate for Dynamic controls across the postback

      【讨论】:

        【解决方案4】:

        1) 您需要在页面上启用视图状态。

        2) 像这篇文章中提到的其他人一样,您需要重新创建动态加载的用户控件。

        这是一个例子:

        protected void Page_Load(object sender, EventArgs e)
        {
            Render();
        }
        

        渲染方法:

        private void Render()
        {
            var list = Helpers.Content.Lists.GetListByTitle();
            if (list != null && list.Items.Count > 0)
            {
                this.divContainer.Controls.Clear();
                var i = 1;
                list.Items.ForEach(question =>
                                       {
                                           this.divContainer.Controls.Add(RenderQuestion(question, i));
                                           i++;
                                       });
            }
        }
        

        渲染问题方法:

            private Control RenderQuestion(Entities.Modules.Item question, int count)
            {
                // Create question control
                var q = (Custom.Widgets.ActionPlans.New.Question)LoadControl("~/Custom/Widgets/ActionPlans/New/Question.ascx");
        
                // Render question
                q.Render(question, count);
                return q;
            }
        

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 2016-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多