开发者

User control based on repeater

I develop user control based on repeater. It must contains predefined checkBox for each row.

开发者_如何学编程

Any suggestions?


Perhaps, something like this?

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>


you can use the Repeater's ItemTemplate, to put necessary controls. Below is an example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜