基于中继器的用户控制

时间:2011-04-19 07:09:02

标签: asp.net user-controls webforms repeater

我基于转发器开发用户控件。它必须包含每行的预定义复选框。

有什么建议吗?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

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