【问题标题】:ASP.NET Gridview Paging ProblemASP.NET Gridview 分页问题
【发布时间】:2010-10-09 10:24:33
【问题描述】:

我有一个网格视图,它使用存储过程在代码隐藏中进行数据绑定。我也在代码中处理 Paging 事件,但是每当我单击页码时,我都会不断获得空数据模板而不是更多行。有什么建议吗?

编辑:更改页面索引后,我正在重新绑定 gv 的数据源。

这是我的代码 - 我有一个下拉列表来确定数据源是什么:

Protected Sub ddlProjectForm_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProjectForm.SelectedIndexChanged
    Dim strProjectFormID As String = Me.ddlProjectForm.SelectedValue
    Dim conn As New SqlConnection(WebConfigurationManager.ConnectionStrings("Conn").ConnectionString)
    Dim cmd As New SqlCommand()
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet

    If strProjectFormID <> "Select" Then
        Try
            Using conn
                conn.Open()

                With cmd
                    .Connection = conn
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "sp_GetAllFormData"
                    .Parameters.AddWithValue("@projectFormID", strProjectFormID)
                End With

                da.SelectCommand = cmd
                da.Fill(ds)

                Me.gvAllSentData.DataSource = ds.Tables(0)
                Me.gvAllSentData.DataBind()
                Me.gvAllSentData.Visible = True
            End Using
        Catch sqlEx As SqlException
            Dim newError As New ErrorLogger(Me.Page.Title, sqlEx.Message, Session("UserName"))
            newError.LogError()

            Trace.Write(sqlEx.Message)
            Me.lblBadFeedback.Visible = True
            Me.lblBadFeedback.Text = "We're sorry - an error has occurred.  It has been logged and will be reviewed by the site admin."
        Catch ex As Exception
            Dim newError As New ErrorLogger(Me.Page.Title, ex.Message, Session("UserName"))
            newError.LogError()

            Trace.Write(ex.Message)
            Me.lblBadFeedback.Visible = True
            Me.lblBadFeedback.Text = "We're sorry - an error has occurred.  It has been logged and will be reviewed by the site admin."
        End Try
    Else
        Me.gvAllSentData.DataSource = Nothing
        Me.gvAllSentData.Visible = False
    End If

End Sub

Protected Sub gvAllSentData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvAllSentData.PageIndexChanging
    Me.gvAllSentData.PageIndex = e.NewPageIndex
    Me.gvAllSentData.DataBind()
End Sub

【问题讨论】:

    标签: asp.net gridview custompaging


    【解决方案1】:

    您正在重新绑定一个空数据源。您的代码应为:

    Protected Sub gvAllSentData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvAllSentData.PageIndexChanging
        Me.gvAllSentData.PageIndex = e.NewPageIndex
        Me.gvAllSentData.DataSource = __The_Data_To_Bind__
        Me.gvAllSentData.DataBind()
    End Sub
    

    【讨论】:

      【解决方案2】:

      虽然听起来很傻,但您还记得再次调用您的数据绑定代码吗?当发生分页时,会进行回发,并且您的 GridView 的数据源会丢失,因此您需要再次重新绑定数据,以便 GridView 可以根据页面加载适当的数据。

      我认为这是我写的第二个或第三个 ASP.NET 应用程序,然后我记得在编写代码 8^D 时第一次重新数据绑定

      【讨论】:

      • 是的,我在设置新的页面索引后绑定。
      • 嗯,可以编辑您最初的问题并将您的代码发布到那里吗?
      【解决方案3】:

      听起来确实像一个数据绑定问题。也许你的数据源在回发后是空的?

      排序等其他事件是否有效?

      你能在回发后单步执行代码的绑定部分并确认数据都在那里吗?

      您有任何可以发布的代码吗?也许您应该创建一个测试工具,其中只包含无法尝试跟踪问题的代码部分。

      【讨论】:

        猜你喜欢
        • 2010-10-13
        • 1970-01-01
        • 2013-02-12
        • 2011-10-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多