thevan
thevan

Reputation: 10364

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?

Upvotes: 0

Views: 4949

Answers (3)

ellickakudy rajeesh
ellickakudy rajeesh

Reputation: 747

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

Upvotes: 1

Mohan
Mohan

Reputation: 969

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.

Upvotes: 0

maka
maka

Reputation: 566

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.

Upvotes: 0

Related Questions