ASP.NET Gridview 分页问题
我有一个 gridview,它使用存储过程在代码隐藏中进行数据绑定。 我也在代码中处理分页事件,但每当我单击页码时,我都会不断获得空数据模板而不是更多行。 有什么建议么?
编辑:更改页面索引后,我将重新绑定 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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在重新绑定一个空数据源。 您的代码应为:
You are rebinding an empty datasource. Your code should read:
尽管听起来很愚蠢,但您是否记得再次调用数据绑定代码? 当分页发生时,会进行回发,并且 GridView 的数据源会丢失,因此您需要再次重新绑定数据,以便 GridView 可以根据页面加载适当的数据。
我认为这是我编写的第二个或第三个 ASP.NET 应用程序,然后我在编写代码时第一次记得重新数据绑定 8^D
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
这听起来确实像是一个数据绑定问题。 也许你的数据源在回发后是空的?
其他事件(例如排序)是否有效?
您能否在回发后单步执行代码的绑定部分并确认数据全部存在?
您有可以发布的代码吗? 也许您应该创建一个测试工具,其中仅包含无法尝试跟踪问题的代码部分。
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.