简体   繁体   English

在ASP.NET C#中动态添加更新面板

[英]Adding update panel dynamically in asp.net c#

  <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>

This is my C# code 这是我的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);            
    }

actually it gives the labels & checkboxes according to my condition before I create the Update Panel dynamically... 实际上,在我动态创建更新面板之前,它会根据我的情况给出标签和复选框。

It gives only one Label & one checkbox. 它仅提供一个标签和一个复选框。 Is this correct way to create Update Panel dynamically? 这是动态创建更新面板的正确方法吗?

Create an UpdatePanel instance then create your "child" controls and add them to the Controls collection of the ContentTemplateContainer property of the UpdatePanel. 创建一个UpdatePanel实例,然后创建“子”控件,并将其添加到UpdatePanel的ContentTemplateContainer属性的Controls集合中。 Finally add the UpdatePanel to the Form and you're done. 最后,将UpdatePanel添加到窗体中,然后完成。 You'll need a on your page. 您的页面上需要一个。

Check this Link : Link that shows a simple example. 检查此Link:显示一个简单示例的链接。

It could be that you are adding controls at the wrong page event. 可能是您在错误的页面事件中添加了控件。 Try to use Init or PreInit event. 尝试使用Init或PreInit事件。

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

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