【问题标题】:GridView1.SelectedRow returning nothingGridView1.SelectedRow 什么都不返回
【发布时间】:2010-09-13 11:18:17
【问题描述】:

我的问题是,在下面的代码中,SelectedRow 属性没有返回任何内容。我在运行时手动将项目绑定到 GridView,并且 autogeneratecolumns 和 autogenerateselectbutton 属性为 true。

我认为问题在于单击 Select 命令时有一个 PostBack。

非常感谢。

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

【问题讨论】:

    标签: vb.net gridview postback


    【解决方案1】:

    您想要的行可以通过 e(GridViewCommandEventArgs 参数)访问,特别是 e.CommandArgument 的值,它将具有行索引:

    来自here

    确定行的索引 引发事件,使用 事件的 CommandArgument 属性 传递给事件的参数。 ButtonField 类自动 填充 CommandArgument 属性 具有适当的索引值。为了 其他命令按钮,您必须 手动设置 CommandArgument 命令按钮的属性。为了 例如,您可以设置 当 GridView 控件没有分页 已启用。

    【讨论】:

    • 作为临时解决方案,我使用 SelectedIndexChanged 事件来获得我想要的,CommandArgument 属性并没有真正帮助。非常感谢。
    【解决方案2】:

    所选行在 GridViewCommandEventArgs 参数中。

    【讨论】:

    • 该行尚未被选中。
    【解决方案3】:

    除了添加 CommandArgument 之外,您还需要将代码更改为以下内容。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-18
      • 2018-09-10
      • 2017-02-14
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多