I have a web app(ASP.NET 2.0 using C#) that I am working on. In it, I have a gridview with a hyperlinkfield on a page(My_Page.aspx). When the Hyperlinkfield is clicked, it shows details on the same page.
<asp:HyperLinkField DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="My_Page.aspx?id={0}"
DataTextField="NAME"
HeaderText="Item1"
SortExpression="NAME" />
I want to know how to find the Index of the row in which the Hyperlink is clicked, because I want to change its style, so that the user knows which row was clicked.
OR
How would I change the style of it when the user clicks hyperlink in the gridview.
Thank you.
In your example, the "index" or rather "id" of the hyperlink that was clicked will be in Request.QueryString["id"]
You could compare the ID from the querystring with the ID of the row you are binding to in the RowDataBound event.
Alternatively you could use a <%# DataBinder.Eval %> in your aspx to set the style based upon the ID field and the query string.
EDIT: Code Sample, try adding this to your code behind.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(Request.QueryString["id"] != null &&
Request.QueryString["id"] == DataBinder.Eval(e.Row.DataItem, "id").ToString())
{
e.Row.Style.Add("font-weight", "bold");
}
}
}