Gridview DataKeyNames

时间:2014-01-09 17:56:12

标签: asp.net vb.net

有没有办法将DataKeyname“Number”包含在gridview selectedindexchanged中的response.redirect上,以便我可以将它传递到下一页?我目前正在使用此代码传递数据,并且gridview当前已设置为显示行中的数字。我想隐藏“数字”列,而是使用datakeyname。谢谢!

Response.Redirect("RoundingEntry.aspx?Room=" & GridView1.SelectedRow.Cells(1).Text & "&Name=" & GridView1.SelectedRow.Cells(2).Text & "&Number=" & GridView1.SelectedRow.Cells(3).Text & "&Member=" & DDMember.SelectedValue & "&Unit=" & DDUnit.SelectedValue)

1 个答案:

答案 0 :(得分:0)

由于您处于SelectedIndexChanged事件中,因此可以使用GridView.SelectedIndex属性从GridView.DataKeys集合中获取写入数据密钥。

我是否也建议清理它,以便它不是一长串的代码?

Sub CustomersGridView_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    Dim room As String = GridView1.SelectedRow.Cells(1).Text
    Dim name As String = GridView1.SelectedRow.Cells(2).Text
    ' This is the one I've changed, to grab the DataKey of the selected row
    Dim number As String = GridView1.DataKeys(GridView1.SelectedIndex).Value.ToString()
    Dim member As String = DDMember.SelectedValue.ToString()
    Dim unit As String = DDUnit.SelectedValue.ToString()

    Response.Redirect("RoundingEntry.aspx?Room=" & room & _
                                        "&Name=" & name & _
                                        "&Number=" & number & _
                                        "&Member=" & member & _
                                        "&Unit=" & unit)
End Sub