动态绑定复选框

动态绑定复选框,选中某个复选框获取数据ID值

前台代码:

<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="1" style="width:1000px;text-align:center;border-collapse:collapse;">
<thead style="background-color:gray;">
<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值
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值