如何在绑定时根据值检查转发器内的复选框?
我有一个中继器,里面有一个复选框。现在我想根据列值(0/1)检查它。我已经通过中继器的itemDataBound事件尝试过。如果行的值为 1,那么它会做什么,然后它会选中所有复选框,如果第一个复选框未选中,则它会取消选中所有复选框。 我的代码是:- `
<td align="center">
<asp:CheckBox ID="chk" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>`
ItemDataBound 事件代码是:-
protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataTable dt = new DataTable();
dt = obj.abc(id);
if (dt.Rows.Count > 0)
{
CheckBox chk = (CheckBox)(e.Item.FindControl("chk"));
if (chk != null)
{
if (Convert.ToInt32(dt.Rows[0]["xyz"]) == Convert.ToInt32("0"))
{
chk.Checked = false;
}
else
{
chk.Checked = true;
}
}
}
}
I have a repeater and inside it i have a checkbox. Now i want to check it according to columns value(0/1). I have tried it through the itemDataBound event of the repeater. what its doing if the rows has value 1 then its checked all checkbox and if first checkbox is unchecked then its unchecked all.
my code is:-
`
<td align="center">
<asp:CheckBox ID="chk" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>`
The ItemDataBound events code is :-
protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
DataTable dt = new DataTable();
dt = obj.abc(id);
if (dt.Rows.Count > 0)
{
CheckBox chk = (CheckBox)(e.Item.FindControl("chk"));
if (chk != null)
{
if (Convert.ToInt32(dt.Rows[0]["xyz"]) == Convert.ToInt32("0"))
{
chk.Checked = false;
}
else
{
chk.Checked = true;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有很多方法可以做到这一点。
您可以编写内联 ASP.NET:
正如您所提到的,您可以使用
repeater_ItemDataBound
查找每行的复选框,并相应地设置其值:另一种方法可能是注入隐藏字段中的布尔状态(此处为婚姻状态)并将其发送到客户端,然后在客户端使用 jQuery(或任何其他 JavaScript 框架)根据隐藏字段的值更新复选框的选中状态字段:
There are many ways to do that.
You can write inline ASP.NET:
As you have mentioned, you can use
repeater_ItemDataBound
to find the check-box of each row, and set its value accordingly:Another way could be to inject the Boolean state (here, the marriage state) in a hidden field and send it to the client, then on client-side, using jQuery (or any other JavaScript framework), update check-boxes' checked state according to the value of those hidden fields: