GridView点击行,选中模版列中CheckBox

时间:2023-03-09 15:18:51
GridView点击行,选中模版列中CheckBox
<asp:TemplateField ItemStyle-Width="40px" HeaderText="选择" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox runat="server" ID="CheckBox1" />
</ItemTemplate>
</asp:TemplateField>
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = (CheckBox)e.Row.FindControl("CheckBox1");
e.Row.Attributes["onclick"] = chk.ClientID + ".checked=!" + chk.ClientID + ".checked;";
//停止事件冒泡,防止选中状态混乱
chk.Attributes["onclick"] = "window.event.cancelBubble = true;";
}
}