【问题标题】:Adding update panel dynamically in asp.net c#在asp.net c#中动态添加更新面板
【发布时间】:2013-12-18 11:39:26
【问题描述】:
  <div class="controls">
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
       <ContentTemplate>
          <h1>Create a new event                                                
             <asp:TextBox ID="EventName_TB" runat="server" CssClass="control-label"></asp:TextBox>
              starting on 
            <asp:TextBox ID="StartDate_TB" runat="server" Style="color: #727272 !important; font-size: 24px; font-weight: 100;" CssClass="span2 input-xlarge datepicker" placeholder="mm/dd/yy"></asp:TextBox>
               for
               <asp:DropDownList ID="EventDuration_DDL" runat="server" Style="color: #727272 !important; font-size: 24px; font-weight: 100;" CssClass="span1" AutoPostBack="true">
                    <asp:ListItem>1</asp:ListItem>
                    <asp:ListItem>2</asp:ListItem>
                    <asp:ListItem>3</asp:ListItem>
                    <asp:ListItem>4</asp:ListItem>
                    <asp:ListItem>5</asp:ListItem>
                    <asp:ListItem>6</asp:ListItem>
                    <asp:ListItem>7</asp:ListItem>
                </asp:DropDownList>
             days. </h1>
          </ContentTemplate>
       </asp:UpdatePanel>
   </div>

这是我的 C# 代码

private void EventDuration()
    {
        Labeldiv.Controls.Clear();
        DateTime dt = DateTime.Parse(StartDate_TB.Text);
        int Duration = Int32.Parse(EventDuration_DDL.SelectedItem.ToString());
        UpdatePanel up = new UpdatePanel();
        up.ID = "UpdatePanel8";
        up.UpdateMode = UpdatePanelUpdateMode.Conditional;

        for (int id = 0; id < Duration; id++)
        {               
            Label NewLabel = new Label();
            NewLabel.ID = "Label" + id;
            var eventDate = dt.AddDays(id);
            NewLabel.Text = eventDate.ToLongDateString();

            CheckBox newcheck = new CheckBox();
            newcheck.ID = "CheckBox" + id;
            newcheck.AutoPostBack = true;                
            newcheck.CheckedChanged += new EventHandler(newcheck_CheckedChanged);
            up.ContentTemplateContainer.Controls.Add(NewLabel);
            this.Labeldiv.Controls.Add(NewLabel);                
            up.ContentTemplateContainer.Controls.Add(newcheck);
            this.Labeldiv.Controls.Add(new LiteralControl("<br/>"));                
        }            
        this.Labeldiv.Controls.Add(up);            
    }

实际上,在我动态创建更新面板之前,它会根据我的情况给出标签和复选框...

它只提供一个标签和一个复选框。这是动态创建更新面板的正确方法吗?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    创建一个 UpdatePanel 实例,然后创建您的“子”控件并将它们添加到 UpdatePanel 的 ContentTemplateContainer 属性的 Controls 集合中。最后将 UpdatePanel 添加到表单中,您就完成了。您的页面上需要一个。

    检查这个Link : Link that shows a simple example.

    【讨论】:

      【解决方案2】:

      可能是您在错误的页面事件中添加了控件。 尝试使用 Init 或 PreInit 事件。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多