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

這是我的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);            
    }

實際上,在我動態創建更新面板之前,它會根據我的情況給出標簽和復選框。

它僅提供一個標簽和一個復選框。 這是動態創建更新面板的正確方法嗎?

創建一個UpdatePanel實例,然后創建“子”控件,並將其添加到UpdatePanel的ContentTemplateContainer屬性的Controls集合中。 最后,將UpdatePanel添加到窗體中,然后完成。 您的頁面上需要一個。

檢查此Link:顯示一個簡單示例的鏈接。

可能是您在錯誤的頁面事件中添加了控件。 嘗試使用Init或PreInit事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM