当 GridView 页面更改时,使用哪个事件来修改服务器控件?

Which event to use to modify server controls when GridView Page changes?

我正在实施过滤器,在 Page_PreRender 上检查哪些行符合我设置的条件。

  Protected Sub Page_PreRender(sender As Object, e As EventArgs) Handles Me.PreRender
    If Session("Hotel") <> "Todos" Then
        CType(GridView1.HeaderRow.Cells(1).FindControl("DropDownList2"), DropDownList).SelectedValue = Session("Hotel")
    End If
    If Session("Departamento") <> "Todos" Then
        CType(GridView1.HeaderRow.Cells(3).FindControl("DropDownList1"), DropDownList).SelectedValue = Session("Departamento")
    End If
    If Session("Estado") <> "Todos" And Session("Estado") <> "" Then
        CType(GridView1.HeaderRow.Cells(7).FindControl("DropDownList3"), DropDownList).SelectedValue = Session("Estado")
    End If
       ApplyFilters()
  End Sub

过滤器保存在会话变量中(在 DropDownListSelectedIndexChanged 上,这会导致回发),然后我获取(如上所示)并设置它们对应的字段,无论他们在回发之前是什么。完成后,通过调用 ApplyFilters() 重新应用过滤器。

如果您想知道过滤器的工作原理:

 For Each Row As GridViewRow In GridView1.Rows
            If CType(Row.Cells(3).FindControl("Label3"), Label).Text <> Filtro Then
                Row.Visible = False
            End If
        Next

一切似乎都正常,直到我切换到另一个 GridView 页面,过滤器没有重新应用并且所有对应的 DropDownList 值都设置为默认值。刷新页面后一切恢复正常。似乎换页不会触发 Page_PreRender。 在这种情况下我应该在哪里实施我的过滤器?

切换到 GridView 中的另一个页面不会触发 Page_PreRender,因此如果您想编辑 GridView 的内容以进行过滤,请使用 GridView_PreRender

Protected Sub GridView1_PreRender(sender As Object, e As EventArgs) Handles GridView1.PreRender
        If Session("Hotel") <> "Todos" Then
            CType(GridView1.HeaderRow.Cells(1).FindControl("DropDownList2"), DropDownList).SelectedValue = Session("Hotel")
        End If
        If Session("Departamento") <> "Todos" Then
            CType(GridView1.HeaderRow.Cells(3).FindControl("DropDownList1"), DropDownList).SelectedValue = Session("Departamento")
        End If
        If Session("Estado") <> "Todos" Then
            CType(GridView1.HeaderRow.Cells(7).FindControl("DropDownList3"), DropDownList).SelectedValue = Session("Estado")
        End If
        ApplyFilters()
    End Sub