2013-12-18 57 views
0
<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);    
    } 

實際上它給人的標籤&複選框根據我的條件之前,我創建動態更新面板...

它只給出一個標籤&一個複選框。這是動態創建更新面板的正確方法嗎?

回答

0

創建一個UpdatePanel實例,然後創建您的「子」控件並將它們添加到UpdatePanel的ContentTemplateContainer屬性的Controls集合中。最後,將UpdatePanel添加到表單,然後完成。你需要在你的頁面上。

入住這Link : Link that shows a simple example.

+0

你能給我的代碼根據我的例子..... – THOR

0

這可能是因爲你在錯誤的頁面事件添加控件。 嘗試使用Init或PreInit事件。

+0

我的EventDuration()在Page_Load().. – THOR

+0

看看http://msdn.microsoft.com/en-us/library /ms178472%28v=VS.100%29.aspx –

+0

這裏建議使用PreInit事件來創建動態控件。 –