基于中继器的用户控制

发布于 2024-11-01 21:47:05 字数 69 浏览 1 评论 0原文

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

有什么建议吗?

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

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-11-08 21:47:05

也许,类似这样的事情?

默认.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>

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>
掀纱窥君容 2024-11-08 21:47:05

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文