0

I have a page with GridView. The GridView has select button. Normally I use GridView's selected index changed event to do all kinds of operations when user clicks select button. But now I want to do some operations in Page_Load event based on grid view's selected row. Since the Selected_Index_changed event occurs after Page_Load how do I know following things in page load event.

I checked the asp lifecycle and this other question but I dont know how to do this.

Community
  • 1
  • 1
Redder
  • 146
  • 1
  • 5
  • 17
  • Why do you need to do that in `Page_Load`, what are you trying to do? Why can't you use the `SelectedIndexChanged` event? Use the appropriate event handler(perhaps a button-click event) and then access it via [`GridView.SelectedRow`](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedrow(v=vs.110).aspx). Do not use `Page_Load` for this. – Tim Schmelter Jan 28 '15 at 10:16
  • Because I'm creating dynamically some buttons based on the user's selection and I need to assign "click event" to those buttons. Due to asp lifecycle, these events must be assigned on page_load or before. – Redder Jan 28 '15 at 10:21
  • Not only the events must be added but even the controls themself need to be re-created on every postback. – Tim Schmelter Jan 28 '15 at 10:22

1 Answers1

0

How about using a QueryString to transmit which row was selected and then in the Page_Load event get the QueryString parameters? This is an example.

Protected Sub LinkButton1_Command(sender As Object, e As CommandEventArgs)
    Dim UserId As Integer = e.CommandArgument 'Here goes whatever value you're trying to pass
    Response.Redirect("~/OtherPage.aspx?UserId=" _
                             & UserId)
End Sub

This is in the OtherPage.aspx

 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    UserId = Request.QueryString("UserId")
    'Your code
end sub