【问题标题】:C# Change the background color of a textbox on a gridviewC#更改gridview上文本框的背景颜色
【发布时间】:2016-11-14 16:34:56
【问题描述】:
我在每行的一个单元格中有一个带有TextBox 的网格视图。我的每一行都有一个用于输入数据的按钮。所以我知道我在哪一行。我想出了如何设置单元格的背景颜色,但不是TextBox 的背景颜色。有谁知道该怎么做?
grIndex - 是我所在的那一行。
Cells[] - 是单元格所在的列。
这是我用来设置单元格背景颜色的代码。
GridViewListComp.Rows[grIndex].Cells[5].BackColor = Color.Yellow;
提前致谢。
【问题讨论】:
标签:
c#
asp.net
gridview
textbox
【解决方案1】:
您必须使用 FindControl 并将其转换回 TextBox 才能访问它的属性。
TextBox textbox = GridView1.Rows[grIndex].Cells[5].FindControl("TextBox1") as TextBox;
textbox.BackColor = Color.Green;
或者您可以使用OnRowDataBound 事件
protected void GridViewListComp_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox textbox = e.Row.FindControl("TextBox1") as TextBox;
textbox.BackColor = Color.Green;
}
}
【解决方案2】:
我想通了。感谢大家的帮助。
((TextBox)GridViewListComp.Rows[grIndex].FindControl("txtPolicy")).BackColor = Color.Yellow;