C# – Changing Gridview row style onclicking a hyperlink field

asp.netcgridviewhyperlink

I have an ASP.NET(2.0 using C#) web app, in which I have a gridview on a page(My_Page.aspx). The gridview gets its data from an Oracle database. I have a Hyperlinkfield in the gridview, which when clicked, postsback to the same page, but with details of the item clicked(using QueryString).

<asp:HyperLinkField DataNavigateUrlFields="ID" 
                    DataNavigateUrlFormatString="My_Page.aspx?id={0}"
                    DataTextField="NAME" 
                    HeaderText="Item1" 
                    SortExpression="NAME" />

I wanted to know how to change the style of the row in which I clicked the Hyperlink.

Any Ideas?

Thank you.

Best Answer

First, a HyperLink(Field) does generally not post back, but in your case, requests the same page with new parameters.

To set a CSS class for a GridView row, use the RowCreated event to do something like this:

protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
        if (e.Row.DataItem.ToString() == Request["id"])
            e.Row.CssClass = "highlighted-css-class";
}
Related Topic