views:

2916

answers:

2

Hi,

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.

+1  A: 

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";
}
devio
A: 

I suggest you use Javascript to achieve that. Since you can't work with the linkbutton directly, you must navigate in the DOM to the parent TR (TableRow) element and set its class property.

First, you add the onclick attribute (if not possible for HyperLinkField try a simple HyperLink or LinkButton)

Pass the control as a parameter to the Javascript function

onclick="selectRow(this)"


function selectRow(hyperlink) { ... }

I'll let you figure out how to navigate through the DOM to find the parent TR.

A few references:

domtables

domstructure

Hope it helps

Maxime