如何找到在asp.net中检入转发器的复选框?

时间:2014-06-30 15:23:01

标签: c# asp.net checkbox repeater

我想点击btndelete,每个复选框,其中chechek在repeater1中获取列(titr)的值在同一行,但我没有找到选中的复选框,并且不能访问列的值(titr)

  <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate>
                <table>
                    <tr>
                        <td>&nbsp;</td>
                        <td>subject</td>

                    </tr>
                    </HeaderTemplate>
            <ItemTemplate>
                          <tr>
                              <td>
                                  <asp:CheckBox ID="CheckBox1" runat="server" />
                              </td>
                              <td id="checkvalue" >
                                  <%# Eval("titr") %>
                              </td>


                          </tr>

            </ItemTemplate>
            <FooterTemplate></table></FooterTemplate>
        </asp:Repeater>

 <asp:Button ID="btndelete" runat="server" Text="delete" OnClick="btndelete_Click" />

.cs代码是:

protected void btndelete_Click(object sender, EventArgs e)
        {


        }

2 个答案:

答案 0 :(得分:2)

您可以使用linq获取每个选中复选框的repeaterItem列表,以便:

    List<RepeaterItem> selectedItems = Repeater1.Items.Cast<RepeaterItem>().Where(x => ((CheckBox)x.FindControl("CheckBox1"))
.Checked).ToList();

然后,您可以遍历列表并获取每个选定行的titr值,但您必须将某个服务器控件设置为等于&#34; titr&#34;绑定控件时,如:

<asp:literal id="literal1" runat="server"><%# Eval("titr") %></asp:literal>

这样你可以在迭代列表后找到这个值,如下所示:

List<string> titrValues = selectedItems.Select(t => ((Literal)t.FindControl("literal1).Text));

您可能需要进行一些调整,但这应该为您获得具有选定值的每行的titr值。

答案 1 :(得分:2)

Aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" runat="server" >
            <ItemTemplate>
                <div>
                    <asp:CheckBox ID="CategoryID" runat="server" Text='<%# Eval("val") %>' />
                </div>
            </ItemTemplate>
        </asp:Repeater>
        <asp:Button Text="Click" OnClick="Button2_Click" runat="server" />
    </form>
</body>
</html>

CS代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              DataTable dt = new DataTable();
              dt.Columns.Add("val", typeof(string));

            for (int i = 0; i < 10; i++)
              dt.Rows.Add("testing" + i.ToString());

       Repeater1.DataSource = dt;
       Repeater1.DataBind();
           }
        }     

    protected void Button2_Click(object sender, EventArgs e)
            {
                string Rpt = "Repeater Items Checked:<br />";
                for (int i = 0; i < Repeater1.Items.Count; i++)
                {
                    CheckBox chk = (CheckBox)Repeater1.Items[i].FindControl("CategoryID");
                    if (chk.Checked)
                    {
                        Rpt += (chk.Text + "<br />");
                    }
                }
                Response.Write(Rpt);
            }

您还可以使用断点来检查获取值....

参与:http://www.codeproject.com/Questions/534719/GetplusSelectedplusCheckboxesplusinplusASPplusRepe