Search code examples
c#asp.netgridviewrowcommand

How to get GridView's LinkButton ForeColor in RowCommand Event?


I have a GridView with the following columns.

 <Columns>
     <asp:TemplateField HeaderText="Item Description">
     <ItemTemplate>
     <asp:Label ID="lblgvItemName" runat="server" Text='<%# Bind("ItemName") %>'></asp:Label>
     </ItemTemplate>
     </asp:TemplateField>
    <asp:BoundField DataField="IssueQty" HeaderText="Issue Qty" />
    <asp:TemplateField>
    <ItemTemplate>
       <asp:LinkButton ID="lnkReturn" runat="server" CommandName="Return" CommandArgument='<%# Eval("ItemName") + "," + Eval("IssueQty") + %>' Text="Return" Font-Bold="true" ForeColor="Red">
       </asp:LinkButton>
    </ItemTemplate>
 </Columns>

In that I need the get the ForeColor of the LinkButton in the RowCommand event of the GridView. Based on the ForeColor, I am doing some validation.

I tried like this,

 string Color = ((LinkButton)gvRIVDetails.Rows[Convert.ToInt32(e.CommandArgument.ToString()].FindControl("lnkReturn")).ForeColor;

But I have already specified ItemName and IssueQty in the Command Argument. So it throws the exception. How to find the ForeColor of the LinkButton?


Solution

  • This will help you. Please take look.

                GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
    
            LinkButton lstText = (LinkButton)row.FindControl("lnkReturn");
    
            string text = lstText.ForeColor.ToString();