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