2015-05-09 126 views
0

我有一個要求,添加一個包含用戶控件(文本框)+ 2 Gridviews的面板和他們自己的項目模板+在這些控件上運行的其他驗證,在Web窗體上動態地由按鈕點擊產生。所以每當用戶點擊一個按鈕「添加面板」時,就會產生一個新的面板和上面的控件。在按鈕上添加動態控件(控件集)點擊

我正在嘗試數據列表和中繼器的路由,但將數據綁定到上述控件正成爲一項挑戰。我想在未來之前調查其他領域來實現這一目標。

任何幫助,鏈接,建議或指針,將不勝感激?

回答

1

只是爲了讓你開始。

  • 用戶控件:DynamicUC.ascx
  • 頁使用該用戶控制:DynamicPage.aspx

DyanamicUC.ascx

<div style="float: left"> 
    <asp:TextBox ID="tbMyTextBox" runat="server" /> 

    <asp:GridView runat="server" ID="gvNumbers" AutoGenerateColumns="False"> 
     <Columns> 
      <asp:BoundField DataField="Serial" HeaderText="Seiral" /> 
      <asp:TemplateField HeaderText="Item Name"> 
       <ItemTemplate> 
        <%# Eval("Item") %> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

</div> 

DynamicUC.ascx.cs

public partial class DynamicUC : UserControl 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     public void PopulateData(string value) 
     { 
      tbMyTextBox.Text = value; 
      gvNumbers.DataSource = Enumerable.Range(1, 5).Select(i => new { Serial = i, Item = "Item " + i }); 
      gvNumbers.DataBind(); 
     } 

     public string GetData() 
     { 
      return Server.HtmlEncode(tbMyTextBox.Text); 
     } 
    } 

DynamicPage.aspx

<asp:Button ID="btnAddUC" Text="Add UC" runat="server" OnClick="btnAddUC_Click" /> 
<asp:Button ID="btnGetUCValues" Text="Get UC Values" runat="server" OnClick="btnGetUCValues_Click" /> 

<asp:Panel runat="server" ID="pnlDynamicUCPanel" Style="overflow: auto;"> 
</asp:Panel> 
<asp:Label ID="lblUCValues" runat="server" Style="clear: both;" /> 

DynamicPage.aspx.cs

public partial class DynamicPage : System.Web.UI.Page 
{ 
    private int NumberOfDynamicControls 
    { 
     get 
     { 
      var numberOfDynamicControls = ViewState["__dynamicUCCount"]; 
      if (numberOfDynamicControls != null) 
      { 
       return (int)numberOfDynamicControls; 
      } 
      return 0; 
     } 
     set 
     { 
      ViewState["__dynamicUCCount"] = value; 
     } 
    } 
    private List<DynamicUC> _dynamicUCList; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     RestoreDynamicUC(); 
    } 

    protected void btnAddUC_Click(object sender, EventArgs e) 
    { 
     CreateDyanamicUC(NumberOfDynamicControls); 
     NumberOfDynamicControls++; 
    } 

    private void RestoreDynamicUC() 
    { 
     if (NumberOfDynamicControls == 0) 
      return; 
     for (int i = 0; i < NumberOfDynamicControls; i++) 
     { 
      CreateDyanamicUC(i); 
     } 
    } 

    private void CreateDyanamicUC(int dataIndex) 
    { 
     if (_dynamicUCList == null) 
     { 
      _dynamicUCList = new List<DynamicUC>(); 
     } 
     var dynamicUC = LoadControl("DynamicUC.ascx") as DynamicUC; 
     dynamicUC.PopulateData("Data " + dataIndex); 
     pnlDynamicUCPanel.Controls.Add(dynamicUC); 
     _dynamicUCList.Add(dynamicUC); 
    } 

    protected void btnGetUCValues_Click(object sender, EventArgs e) 
    { 
     var valuesText = ""; 
     if (_dynamicUCList != null) 
     { 
      valuesText = string.Join(", ", _dynamicUCList.Select(duc => duc.GetData())); 
     } 
     lblUCValues.Text = "UC Values: " + valuesText; 
    } 
} 

有很多解釋;但我恐怕沒有多少時間。但是這應該給一些提示。