动态绑定复选框,选中某个复选框获取数据ID值
前台代码:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" style="1000px;text-align:center;border-collapse:collapse;">
<thead style="">
<tr>
<th><asp:CheckBox ID="CheckBoxAll" runat="server" />全选</th>
<th>内容</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<asp:Panel ID="plItem" runat="server">
<tr>
<td><asp:CheckBox ID="ckSelect" ToolTip='<%#Eval("id") %>' Text="" runat="server"/></td>
</td>
<td><%#Eval("name") %></td>
</tr>
</asp:Panel>
<asp:Panel ID="plEdit" runat="server">
<tr>
<td><asp:Label runat="server" ID="Label1" Text='<%#Eval("id") %>'></asp:Label></td>
<td><asp:TextBox ID="txtName" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></td>
</tr>
</asp:Panel>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="btnSelect" runat="server" Text="选择" OnClick="btnSelect_Click" />
后台代码:
protected void btnSelect_Click(object sender, EventArgs e)
{
bool result = true;
//遍历repeater控件的itemtemplate模版
foreach (RepeaterItem item in Repeater1.Items)
{
CheckBox cb = (CheckBox)item.FindControl("ckSelect"); //根据控件id获得控件对象,ckSelect是checkBox控件的id
if (cb.Checked == true)
{
//获取id记录,cb.ToolTip里存放的就是记录id
string controlName = cb.ID; //控件名称
string id = cb.ToolTip; //绑定数据ID值
}
}
}