简体   繁体   中英

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

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. Finally add the UpdatePanel to the Form and you're done. You'll need a on your page.

Check this Link : Link that shows a simple example.

It could be that you are adding controls at the wrong page event. Try to use Init or PreInit event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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