我有一个带html表的转发器。在html表中,我有一个带有复选框的表格单元格。
我试图在单击按钮后从用户获取已检查的行,但结果始终为null。
asp.net标记:
<table id="tbl1" class="table">
<tr>
<th>test 1</th>
<th>test 2</th>
<th>test 3</th>
<th>test 4</th>
<th>Select</th>
</tr>
<asp:Repeater ID="rep" runat="server">
<ItemTemplate>
<tr id="tr1" runat="server">
<td>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("test1") %>'>' ></asp:Label>
</td>
<td>
<asp:Label ID="lbl2" runat="server" Text='<%#Eval("test2") %>'>' ></asp:Label>
</td>
<td>
<asp:Label ID="lbl3" runat="server" Text='<%#Eval("test3") %>'>' ></asp:Label>
</td>
<td>
<asp:Label ID="lbl4" runat="server" Text='<%#Eval("test4") %>'>' ></asp:Label>
</td>
<td id="td1" runat="server">
<asp:CheckBox ID="Select" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<asp:Button ID="btnSelect" runat="server" Text="Go" OnClick="btnSelect_Click" />
C#代码:
protected void btnSelect_Click(object sender, EventArgs e)
{
foreach (RepeaterItem rpt in rep.Items)
{
CheckBox ckb = (CheckBox)rpt.FindControl("Select");
if (ckb.Checked) // Always Null
{
//
}
else
{
//
}
}
}
答案 0 :(得分:1)
问题是您在转发器项目中有另一个服务器控件。复选框不直接位于表行中的转发器项目中。您可以像这样提取复选框
CheckBox ckb = (CheckBox)rpt.FindControl("tr1").FindControl("Select");
if (ckb.Checked)
...
当然这很糟糕,因为更改布局会破坏您的代码。要解决这个问题,您可以编写一个递归的FindControl,但它需要更多的工作。
答案 1 :(得分:0)
你是否在PostBack上重新绑定了Repeater的DataSource?这将导致重置器中所有控件的状态被重置。
答案 2 :(得分:0)
问题可能是rpt.FindControl("Select")
仅在rpt
的孩子中搜索。你可以试试这个:
/// <summary>
/// Iterates throug all children and returns all of Type T.
/// </summary>
public static List<T> FindChildrenOfType<T>(Control control) where T : class
{
List<T> controls = new List<T>();
foreach (Control childControl in control.Controls)
{
if (childControl.Controls.Count > 0)
{
controls.AddRange(FindChildrenOfType<T>(childControl, comp));
}
if (childControl is T)
{
controls.Add(childControl as T);
}
}
return controls;
}
像这样使用:
var checkboxes = FindChildrenOfType<CheckBox>(rpt);
答案 3 :(得分:0)
你可以尝试这个......
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