问题
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?
回答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();
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/9512936/how-to-get-gridviews-linkbutton-forecolor-in-rowcommand-event