質問

In my gridview controls are enabled or disabled depending on user roles. I want to change the background color to yellow for enabled controls. I have tried to do it in RowCreated as below but all the cells are enabled at that time.

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
          }

     }
}

Here is an example field in my gridview where I enable or disable the controls.

    <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>
役に立ちましたか?

解決

You can set the BackColor too in using the data binding syntax

<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>

A little ugly, but will work just fine.

他のヒント

You can go through the following step

  1. check the user role on RowDatabound
  2. On RowDatabound change the color of the row

    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
         }
    }
    

You can set css dynamically to the textbox 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>

Try it in RowDataBound event of the Gridview.

Try to use DataBound event to iterate thru all cells and update the bg.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top