设置Gridview控件的背景色,当它被启用或禁用时
问题描述:
在我的gridview控件中,根据用户角色启用或禁用控件。我想将背景颜色更改为黄色以启用控件。我试图在RowCreated
中做到这一点,但当时所有单元格都已启用。设置Gridview控件的背景色,当它被启用或禁用时
protected void begv_OrderDetail_RowCreated(object sender, GridViewRowEventArgs e)
{
foreach (TableCell cell in e.Row.Cells)
{
if (cell.Enabled == true)
{
}
else
{
//Never enters this area
}
}
}
这是我的gridview中的一个示例字段,其中启用或禁用控件。
<asp:TemplateField HeaderText="ReasonCode" SortExpression="ReasonCode">
<HeaderTemplate>
<asp:Label ToolTip="ReasonCode" runat="server" Text="RC"></asp:Label>
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_ReasonCode" onchange="disableNextStatusButtons()" runat="server" Text='<%# Bind("ReasonCode") %>'
Enabled='<%# (Roles.IsUserInRole("İhracat Uzmanı") && Session["Status"].ToString()=="3") %>'
Width="40px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
答
您可以设置BackColor
太使用数据绑定语法
<asp:TextBox ID="txt_ReasonCode"
onchange="disableNextStatusButtons()"
runat="server"
Text='<%# Bind("ReasonCode") %>'
Enabled='<%# (Roles.IsUserInRole("İhracat Uzmanı") && Session["Status"].ToString()=="3") %>'
BackColor='<%# (Roles.IsUserInRole("İhracat Uzmanı") && Session["Status"].ToString()=="3") ? System.Drawing.Color.Red: System.Drawing.Color.Green %>'
Width="40px">
</asp:TextBox>
有点难看,但工作得很好。
答
尝试使用DataBound事件遍历所有单元格并更新bg。
答
你可以通过下面的步骤
- 支票上的RowDataBound 用户角色
-
上的RowDataBound改变行的颜色
protected void RowDataBound(Object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { //check role if (condition) e.Row.BackColor = Color.Red; else e.Row.BackColor = Color.Green; //or set you individual control background //get any control var chk = (CheckBox)e.Row.FindControl("chkb"); //set background chk.BackColor = Color.Red;//etc } }
可以动态地设置CSS到文本框CssClass="yourcss"
<asp:TextBox ID="txt_ReasonCode" onchange="disableNextStatusButtons()" runat="server" Text='<%# Bind("ReasonCode") %>'
Enabled='<%# (Roles.IsUserInRole("İhracat Uzmanı") && Session["Status"].ToString()=="3") %>'
CssClass='<%# (Roles.IsUserInRole("İhracat Uzmanı") && Session["Status"].ToString()=="NormalCss").ToString()=="true"?"yellowcss":"othercss" %>'
Width="40px"></asp:TextBox>
你在哪里启用和禁用控件? – nunespascal 2013-02-27 07:28:02
@nunespascal,我编辑了我的问题,并从我的gridview中添加了一小段代码,您可以在其中看到启用的选项。 – HOY 2013-02-27 07:31:12
您也可以使用相同的语法来设置颜色。看到我的回答 – nunespascal 2013-02-27 07:54:04