2011-04-19 78 views
0

我開發基於中繼器的用戶控制。它必須包含預定義的 checkBox的每一行。基於中繼器的用戶控制

有什麼建議嗎?

+0

你說的預定義是什麼意思?先生,需要多一點的時間繼續下去。 – 2011-04-19 07:19:54

+0

每行必須包含複選框。 – Alexandre 2011-04-19 07:35:57

+0

這沒有幫助。中繼器必須具有預定義的複選框和表格佈局。 – Alexandre 2011-05-09 06:47:38

回答

0

您可以使用Repeater的ItemTemplate來放置必要的控件。下面是一個例子:

<asp: Repeater> 
     <ItemTemplate>  
       <asp:CheckBox ID="cbkWhatever" runat="server" Checked='<%# Eval("CbkDataField") %>' /> 
       All other items for Repeater ... 
     </ItemTemplate> 
</asp:Repeater> 
1

也許,這樣的事情?

Default.aspx的

<%@ Page Language="C#" %> 
<script runat="server"> 
    protected override void CreateChildControls() 
    { 
     base.CreateChildControls(); 
     LoadUserControl(); 
    } 
    private void LoadUserControl() 
    { 
     ph.Controls.Clear(); 
     Control c = LoadControl("~/WebUserControl.ascx"); 
     c.ID = "foo"; 
     ph.Controls.Add(c); 
    } 
</script> 
<html> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:PlaceHolder ID="ph" runat="server" /> 
    </div> 
    <asp:Button runat="server" /> 
    </form> 
</body> 
</html> 

WebUserControl.ascx

<%@ Control Language="C#" %> 
<%@ Import Namespace="System.Data" %> 
<script runat="server"> 
    protected override void OnLoad(EventArgs e) 
    { 
     base.OnLoad(e); 
     if (!IsPostBack) 
     { 
      DataTable dt = GetRawDataTable(); 
      dt.Rows.Add(dt.NewRow()); 
      rpt.DataSource = dt; 
      rpt.DataBind(); 
     } 
    } 
    private static DataTable GetRawDataTable() 
    { 
     DataTable dt = new DataTable(); 
     dt.Columns.Add(new DataColumn("product", typeof(string))); 
     dt.Columns.Add(new DataColumn("checked", typeof(bool), "false")); 
     return dt; 
    } 
    protected void OnBtnAddClicked(object sender, EventArgs e) 
    { 
     DataTable dt = GetRawDataTable(); 
     foreach (RepeaterItem ri in rpt.Items) 
     { 
      TransferFromPage(ri, dt); 
     } 
     dt.Rows.Add(dt.NewRow()); 
     rpt.DataSource = dt; 
     rpt.DataBind(); 
    } 

    private static void TransferFromPage(RepeaterItem ri, DataTable dt) 
    { 
     DataRow row = dt.NewRow(); 
     row["product"] = ((TextBox)ri.FindControl("txtProduct")).Text; 
     row["checked"] = ((CheckBox) ri.FindControl("chk")).Checked; 
     dt.Rows.Add(row); 
    } 

    protected void OnRptItemCmd(object sender, RepeaterCommandEventArgs ea) 
    { 
     switch (ea.CommandName) 
     { 
      case "RemoveRow": 
       DataTable dt = GetRawDataTable(); 
       foreach (RepeaterItem ri in rpt.Items) 
       { 
        if (ri.ItemIndex == ea.Item.ItemIndex) continue; 
        TransferFromPage(ri, dt); 
       } 
       rpt.DataSource = dt; 
       rpt.DataBind(); 
       break; 
     } 
    } 
</script> 
<div> 
    <table> 
    <asp:Repeater ID="rpt" runat="server" OnItemCommand="OnRptItemCmd"> 
     <ItemTemplate> 
      <tr> 
       <td> 
        <asp:TextBox ID="txtProduct" runat="server" Text='<%# Eval("product") %>' /> 
        <asp:CheckBox ID="chk" runat="server" Checked='<%# Eval("checked") %>' /> 
        <asp:Button ID="btnRemove" runat="server" Text="-" CommandName="RemoveRow" /> 
       </td> 
      </tr> 
     </ItemTemplate> 
     <FooterTemplate> 
      <tr> 
       <td> 
        <asp:Button ID="btnAdd" runat="server" Text="+" OnClick="OnBtnAddClicked" /> 
       </td> 
      </tr> 
     </FooterTemplate> 
    </asp:Repeater> 
    </table> 
</div>