GridView1.SelectedRow returning nothing
My problem is that in the below code, the SelectedRow property returns nothing. I manually bind items to GridView at runtime and autogeneratecolumns and autogenerateselectbutton properties are true.
I think the problem is about having a PostBack when the Select command is 开发者_如何学JAVAclicked.
Thanks a lot.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Session("ContactID") = GridView1.SelectedRow.Cells(0).Text()
Response.Redirect("~/ContactAddress.aspx")
End Sub
The row you want is accessible via e (the GridViewCommandEventArgs parameter), specifically the value of e.CommandArgument which will have the row index :
From here :
To determine the index of the row that raised the event, use the CommandArgument property of the event argument that is passed to the event. The ButtonField class automatically populates the CommandArgument property with the appropriate index value. For other command buttons, you must manually set the CommandArgument property of the command button. For example, you can set the CommandArgument to <%# Container.DataItemIndex %> when the GridView control has no paging enabled.
The selected row is in the GridViewCommandEventArgs parameter.
Besides adding the CommandArgument, you need to change your code to the following.
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim rowNumber As Integer = e.CommandArgument
Dim ContactId As Integer = GridView1.Rows(rowNumber).Cells(0).Text
Session("ContactID") = ContactId
Response.Redirect("~/ContactAddress.aspx")
End Sub
精彩评论