Reputation: 2369
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.
Upvotes: 0
Views: 5682
Reputation: 197
My situation:
<asp:GridView ID="gvProjects" runat="server">
<SelectedRowStyle CssClass="current" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="cmdShowProject" runat="server"
OnCommand="cmdShowProject_Command"
CssClass="show-project" CommandName="select"
</ItemTemplate>
</asp:TemplateField>
SelectedRowStyle must be set (either through individual properties or through CssClass). CommandName must be "select" for the Row style to change. The GridView is in an UpdatePanel.
Upvotes: 0
Reputation: 6976
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:
Hope it helps
Upvotes: 0
Reputation: 37215
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";
}
Upvotes: 3