设置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> 
+0

你在哪里启用和禁用控件? – nunespascal 2013-02-27 07:28:02

+0

@nunespascal,我编辑了我的问题,并从我的gridview中添加了一小段代码,您可以在其中看到启用的选项。 – HOY 2013-02-27 07:31:12

+0

您也可以使用相同的语法来设置颜色。看到我的回答 – nunespascal 2013-02-27 07:54:04

您可以设置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> 

有点难看,但工作得很好。

试试在GridView的RowDataBound事件。

+0

这不起作用。 – HOY 2013-02-27 07:21:10

尝试使用DataBound事件遍历所有单元格并更新bg。

你可以通过下面的步骤

  1. 支票上的RowDataBound
  2. 用户角色
  3. 上的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> 
+0

我有这个想法,但是这使得我必须检查角色的条件加倍,而且我不喜欢这样做。 – HOY 2013-02-27 07:27:36

+0

为什么它将支票加倍? – 2013-02-27 07:32:28

+0

因为我使用角色来启用或禁用控件(请参阅我的问题中的编辑部分),所以当我更改同一控件的颜色时,我不想使用相同的条件,因为如果条件更改,则我必须同时更改启用和禁用的位置以及上面指出的位置。 – HOY 2013-02-27 07:35:42