I have a gridview that has a hyperlink in it that I use to create querystring to filter a second gridview on the same page. Is it possible for the hyperlink to select the row that you click on and then stay on that row when it reloads? Right now when I click on it it resets the gridview state and of course doesn't select the row. I've tried a few of the hacks but I couldn't get any to work. Here is the gridview and the code I've tried
Gridview Source
1 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 2 { 3 if (e.Row.RowType == DataControlRowType.DataRow) 4 { 5 e.Row.Attributes.Add("onMouseOver", "this.style.cursor='hand';"); 6 e.Row.Attributes.Add("onClick", Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex.ToString())); 7 } 8 } 9 protected override void Render(HtmlTextWriter htmlTextW) 10 { 11 // .NET will refuse to accept "unknown" postbacks for security reasons. Because of this we have to register all possible callbacks 12 // This must be done in Render, hence the override 13 for (int i = 0; i < GridView1.Rows.Count; i++) 14 { 15 Page.ClientScript.RegisterForEventValidation(new System.Web.UI.PostBackOptions(GridView1, "Select$" + i.ToString())); 16 } 17 // Do the standard rendering stuff 18 base.Render(htmlTextW); 19 } 20 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 21 { 22 if (e.Row.DataItemIndex == -1) 23 return; 24 e.Row.Attributes.Add("onMouseOver", 25 "this.style.cursor='hand';"); 26 e.Row.Attributes.Add("onclick", this.GetPostBackClientEvent(GridView1, "Select$" + e.Row.RowIndex.ToString())); 27 } 28 protected override void Render(HtmlTextWriter writer) 29 { 30 foreach (GridViewRow row in GridView1.Rows) 31 { 32 row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + row.RowIndex.ToString(), true)); 33 } 34 base.Render(writer); 35 }
![]() |
0 |
![]() |
Assuming that you haven't disabled the ViewState, have you tired setting the GridView's SelectedIndex? If you have ViewState active on the control, it should persist between postbacks.
Failure is always an option. Avoid situations where it is the only option.
![]() |
0 |
![]() |
I haven't disabled anything unless it is by default, but how does a hyperlink make it "Select" the current row when I is navigating away? Are you saying that the way it is now should work without any hard coding the select row event? I'm a begginer so not quite sure how to "set the Gridview's SelectedIndex". I'll research that a bit.
![]() |
0 |
![]() |
Start with this:
Go to the GridView Tasks on the top right corner of the gridview (where you can select the datasource) in design mode. Choose to Edit Columns. Look at the Command Fields. There is a Select column you can add. Do that and observe the result. Go back in and change that column to a template field (bottom rigth side of the Edit Columns form).
Go back to the GridView Tasks and edit templates, and choose the item template for the column. You will notice a link button. You can either add a handler for the click or command events if you want to do something servers, or setup the OnClientClick to do something server side.
The key is the CommandName property, "Select", which GridView is designed to handle by setting the SelectedIndex property to the index of the row in which the Link Button appears. If one wants they could do the same in a click or command event handler, but why go through the effort if you don't need to.
Failure is always an option. Avoid situations where it is the only option.
![]() |
0 |
![]() |
Sounds like what I need, thanks for the help. Just weird how you have to convert a select field to a template field instead of just dumping a template field in there to begin with which sounds like the right way but hey I have to think like MS.
![]() |
0 |
![]() |
Actually, you can add any sort of button in there and set its CommandName field to "Select" and it will work. The advantage of using the "packaged" version is that it follows any formatting you do to it, versus having to manually apply it.
Failure is always an option. Avoid situations where it is the only option.
![]() |
0 |
![]() |
I found my problem, it's because I'm using a querystring to filter the child gridview with a hyperlink. I'm using it to navigate to the same page providing the querystring to filter the second gridview. How can I do this using a command button. There is no navigateURL property.
This is what I'm doing now
<Columns>
<asp:TemplateField HeaderText="Year">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "~/OSFileByDate.aspx?Year=" + Eval("y") %>'><%# Eval("y") %> (<%# Eval("NoFiles") %> files)</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
</Columns>
![]() |
0 |
![]() |
Hi barkster,
You can handle SelectedIndexChanging event of GridView:
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Response.Redirect("~/OSFileByDate.aspx?Year=" + ((DataRowView)GridView1.Rows[e.NewSelectedIndex].DataItem)["y"].ToString());
}Thanks,
Qin Dian Tang
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |
If you are navigating back to the same page, there is no need to change destinatination (by default it will post back to itself. You can look at the PostBackURL if it needs to go elsewhere, however). Because your gridview is on the same page you really don't want to do a response redirect, as Qin Dian Tang suggests (at that point you might as well just use the hyper link and avoid the processing time of the event handler). You do, however, want to do something when the select button is clicked.
Using the SelectedIndexChanged of the first gridview is one option. Going the template route and triggering off the Command or Click events are also viable. In any case this is where you want to apply the filtering logic for your circumstances. If the second grid view is bound to an ObjectDataSource you can set the DefaultValue property of any SelectParameters and then call databind, which should make it all work.
Failure is always an option. Avoid situations where it is the only option.
![]() |
0 |
![]() |