2

我有一个使用存储过程在代码隐藏中绑定数据的gridview。我也在代码中处理 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
4

3 回答 3

7

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

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
于 2009-02-23T21:47:00.403 回答
0

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

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

于 2009-02-23T21:45:58.113 回答
0

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

其他事件(如排序)是否有效?

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

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

于 2009-02-23T21:49:39.510 回答