views:

28

answers:

2

Hi there,

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 clicked.

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
A: 

The selected row is in the GridViewCommandEventArgs parameter.

Brad Bruce
Thats right, but what is the problem about that?
ozancali
The row hasn't been selected yet.
Brad Bruce
A: 

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.

CodeByMoonlight
As a temporary solution, I used the SelectedIndexChanged event to get what I want, CommandArgument property wasn't really helping. Thanks a lot.
ozancali