具有可变行数的C#中继器,每个都可以使用CheckBox / Other方法返回不同的字符串 [英] C# Repeater with a Varying Number of Rows Each Able to Return a Different String With a CheckBox/Other Method

查看:75
本文介绍了具有可变行数的C#中继器,每个都可以使用CheckBox / Other方法返回不同的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建的Asp.net网站使用中继器显示有关两个数据库中重复项或缺少项的字符串列表。在SQL语句中并行创建了另一个字符串列表,其中一个SQL语句对应于Repeater列表中相同编号的字符串。列表中的字符串数取决于所选的数据库,范围可以从零到100+。

The Asp.net website I am creating uses a Repeater to display a list of Strings concerning duplicates or missing entries from two databases. There is another list of strings created in parallel of SQL statements with one SQL statement corresponding to the same numbered string in the Repeater list. The number of strings in the lists depends on the databases chosen and can range from zero to 100+.

问题:由于Repeater行的数量未知,因此我试图找到一种方法来生成未知数量的复选框/按钮(每行一个)。单击后,复选框/按钮将在单独的方法的其他列表中找到该行的相应SQL语句。有谁知道如何创建可变数量的复选框?

Question: Since the number of Repeater rows are unknown, I am trying to find some method to generate an unknown number of checkboxes/buttons (one for each row). When clicked, the checkbox/button will find the appropriate SQL statement in the other list for the row in a separate method. Does anyone have an idea as to how a variable number of checkboxes could be created?

推荐答案

这可以通过添加CommandArgument来完成。到一个按钮并为其分配一个命令,而不是一个点击

This can be done with adding a CommandArgument to a button and assigning a Command to it, not a Click.

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("myColumn") %>'></asp:Label>
        <br />
        <asp:Button ID="Button1" runat="server" CommandArgument='<%# Eval("ID") %>' OnCommand="Button1_Command" Text="Button" />
        <hr />
    </ItemTemplate>
</asp:Repeater>

<asp:Label ID="Label2" runat="server" Text=""></asp:Label>

然后在后面的代码中处理 OnCommand

And then in code behind handle the OnCommand

protected void Button1_Command(object sender, CommandEventArgs e)
{
    Label2.Text = e.CommandArgument.ToString();
}

如果您想阅读CheckBox,则需要更多代码

If you want to read a CheckBox, you'll need slighty more code in code behind.

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("field01") %>'></asp:Label>
        <br />
        <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("itemid")) %>' />
        <br />
        <asp:Button ID="Button1" runat="server" CommandArgument='<%# Eval("itemid") %>' OnCommand="Button1_Command" Text="Button" />
        <hr />
    </ItemTemplate>
</asp:Repeater>

protected void Button1_Command(object sender, CommandEventArgs e)
{
    Button btn = sender as Button;
    RepeaterItem item = (RepeaterItem)btn.NamingContainer;
    CheckBox cb = item.FindControl("CheckBox1") as CheckBox;

    Label2.Text = "Item with ID " + e.CommandArgument + " has checkbox " + cb.Checked;
}

这篇关于具有可变行数的C#中继器,每个都可以使用CheckBox / Other方法返回不同的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆