首页 > 编程 > .NET > 正文

GridView中点击CheckBox选中一行来改变此行的颜色

2020-01-17 23:28:05
字体:
来源:转载
供稿:网友
前台:
复制代码 代码如下:

<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;
}
}
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表