【问题标题】:How to find selected GridView row in page_load event?如何在 page_load 事件中找到选定的 GridView 行?
【发布时间】:2015-01-28 10:14:32
【问题描述】:

我有一个带有 GridView 的页面。 GridView 有选择按钮。通常,当用户单击选择按钮时,我使用 GridView 的选定索引更改事件来执行各种操作。但是现在我想根据网格视图的选定行在 Page_Load 事件中做一些操作。由于 Selected_Index_changed 事件发生在 Page_Load 之后,我如何知道页面加载事件中的以下内容。

我检查了asp lifecyclethis 其他问题,但我不知道该怎么做。

【问题讨论】:

  • 为什么要在Page_Load 中这样做,你想做什么?为什么不能使用SelectedIndexChanged 事件?使用适当的事件处理程序(可能是按钮单击事件),然后通过GridView.SelectedRow 访问它。不要为此使用Page_Load
  • 因为我正在根据用户的选择动态创建一些按钮,并且我需要为这些按钮分配“点击事件”。由于 asp 生命周期,这些事件必须在 page_load 或之前分配。
  • 不仅事件必须添加,甚至控件本身也需要在每次回发时重新创建。

标签: asp.net vb.net gridview pageload


【解决方案1】:

如何使用 QueryString 传输选择了哪一行,然后在 Page_Load 事件中获取 QueryString 参数?这是一个例子。

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

这是在OtherPage.aspx中

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

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

【讨论】:

  • 当我可以在 gridview.SelectedIndexChanged 上获得该值时,这发生在 Page_Load 之后。
  • 不,你可以在Page_Load中做Request.QueryString("QueryStringName")。
猜你喜欢
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多