ASP.NET Gridview Paging Problem

二次信任 提交于 2019-12-11 01:13:48

问题


I have a gridview that is databound in the code-behind using a stored procedure. I am handling the Paging event in the code as well, but whenever I click on a page number, I keep getting the empty data template instead of more rows. Any suggestions?

EDIT: I am re-binding the data source of the gv after I change the page index.

Here is my code - I have a dropdown list that determines what the data source is:

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

回答1:


You are rebinding an empty datasource. Your code should read:

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:


As silly as it sounds, are you remembering to call your databinding code again? When the paging occurs, a post back is made, and the datasource for your GridView is lost, so you will need to rebind the data again so that the GridView can load the appropriate data based on the page.

I think it was my second or third ASP.NET app I had written before I remembed to re-databind the first time through when writing code 8^D




回答3:


It does sound like a databinding issue. Maybe your datasource is empty after the postback?

Do other events like sorting work?

Can you step through the binding part of the code after the postback and confirm the data is all there?

Do you have any code you can post? Maybe you should create a test harness containing just the parts of the code that aren't working to try and track down the problem.



来源:https://stackoverflow.com/questions/579503/asp-net-gridview-paging-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!