0

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?

thevan
  • 10,052
  • 53
  • 137
  • 202
  • It throws the following Exception: "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index" – thevan Mar 01 '12 at 08:41

3 Answers3

1

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();
0

Take a look at:

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

If you are getting index out of range, chances are that it is here:

[Convert.ToInt32(e.CommandArgument.ToString())]

I'd suggest a breakpoint/writeline to see what number you are using as your index. e.CommandArgument might not be what you want to do to parse your row index.

maka
  • 566
  • 4
  • 11
0

Very Simple!!!

<asp:TemplateField> 
<ItemTemplate> 
   <asp:LinkButton ID="lnkReturn" runat="server" CommandName="Return" CommandArgument='<%# Eval("ItemName") + "," + Eval("IssueQty") + %>' Text="Return" Font-Bold="true" ForeColor="Red" oncommand="FunctionABC"> 
   </asp:LinkButton> 
</ItemTemplate> 

Now on Command event just write

LinkButton lb=sender as LinkButton;
lb.ForeColor="Violet";    

and It's done.

Mohan
  • 907
  • 5
  • 22
  • 45