GridView中点击CheckBox选中一行来改变此行的颜色
程序员文章站
2024-02-25 22:13:45
前台: 复制代码 代码如下:
前台:
<asp:templatefield headertext="选择">
<itemstyle horizontalalign="center"/> //居中显示
<itemtemplate>
<asp:checkbox id="ckbselect" runat="server" autopostback="true" oncheckedchanged="ckbselect_checkedchanged" />
</itemtemplate>
</asp:templatefield>
后台:
/// <summary>
/// checkbox选中时,改变行颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ckbselect_checkedchanged(object sender, eventargs e)
{
for (int i = 0; i < this.gvstudent.rows.count; i++)
{
checkbox cb = (checkbox)this.gvstudent.rows[i].findcontrol("ckbselect");
if (cb.checked)
{
this.gvstudent.rows[i].backcolor = system.drawing.color.fromname("#e2eaec");
}
else
{
this.gvstudent.rows[i].backcolor = system.drawing.color.empty;
}
}
}
复制代码 代码如下:
<asp:templatefield headertext="选择">
<itemstyle horizontalalign="center"/> //居中显示
<itemtemplate>
<asp:checkbox id="ckbselect" runat="server" autopostback="true" oncheckedchanged="ckbselect_checkedchanged" />
</itemtemplate>
</asp:templatefield>
后台:
复制代码 代码如下:
/// <summary>
/// checkbox选中时,改变行颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ckbselect_checkedchanged(object sender, eventargs e)
{
for (int i = 0; i < this.gvstudent.rows.count; i++)
{
checkbox cb = (checkbox)this.gvstudent.rows[i].findcontrol("ckbselect");
if (cb.checked)
{
this.gvstudent.rows[i].backcolor = system.drawing.color.fromname("#e2eaec");
}
else
{
this.gvstudent.rows[i].backcolor = system.drawing.color.empty;
}
}
}