datagrid自定义分页功能.点击第二页正常.编辑的时候就跑到第一页了.不知道怎么解决..
代码:
Dim cn As New SqlConnection("server=.;database=testDb;uid=sa")
Dim rowCount As Integer
Dim startIndex As Integer
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGridDataBind()
End If
End Sub
Private Sub DataGridDataBind()
Try
Dim da As New SqlDataAdapter("select bookName,UnitsInStock,UnitPrice,bookID,book.publishID,bookImg,book.CategoryID,bookBrief,auther,CategoryName,publishName from book,Categories,publish where book.CategoryID=Categories.CategoryID and book.publishID=publish.publishID", cn)
Dim ds As New DataSet
cn.Open()
da.Fill(ds, startIndex, DataGrid1.PageSize, "book") '指定索引开始取记录
da.Fill(ds, "AllDataTable")
DataGrid1.VirtualItemCount = ds.Tables("AllDataTable").Rows.Count ' 获取自定义实际页数
DataGrid1.DataSource = ds.Tables("book")
rowCount = ds.Tables("book").Rows.Count
DataGrid1.DataBind()
Catch ex As Exception
Response.Write(ex.Message)
Finally
cn.Close()
End Try
End Sub
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
'编辑 DataGrid1.EditItemIndex = e.Item.ItemIndex
Label1.Text = DataGrid1.CurrentPageIndex
btnSelect.Enabled = False
DataGridDataBind()
End Sub
Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
'取消
DataGrid1.EditItemIndex = -1
DataGridDataBind()
btnSelect.Enabled = True
End Sub
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
startIndex = DataGrid1.PageSize * DataGrid1.CurrentPageIndex
DataGridDataBind()
End Sub
代码:
Dim cn As New SqlConnection("server=.;database=testDb;uid=sa")
Dim rowCount As Integer
Dim startIndex As Integer
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGridDataBind()
End If
End Sub
Private Sub DataGridDataBind()
Try
Dim da As New SqlDataAdapter("select bookName,UnitsInStock,UnitPrice,bookID,book.publishID,bookImg,book.CategoryID,bookBrief,auther,CategoryName,publishName from book,Categories,publish where book.CategoryID=Categories.CategoryID and book.publishID=publish.publishID", cn)
Dim ds As New DataSet
cn.Open()
da.Fill(ds, startIndex, DataGrid1.PageSize, "book") '指定索引开始取记录
da.Fill(ds, "AllDataTable")
DataGrid1.VirtualItemCount = ds.Tables("AllDataTable").Rows.Count ' 获取自定义实际页数
DataGrid1.DataSource = ds.Tables("book")
rowCount = ds.Tables("book").Rows.Count
DataGrid1.DataBind()
Catch ex As Exception
Response.Write(ex.Message)
Finally
cn.Close()
End Try
End Sub
Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
'编辑 DataGrid1.EditItemIndex = e.Item.ItemIndex
Label1.Text = DataGrid1.CurrentPageIndex
btnSelect.Enabled = False
DataGridDataBind()
End Sub
Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
'取消
DataGrid1.EditItemIndex = -1
DataGridDataBind()
btnSelect.Enabled = True
End Sub
Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
startIndex = DataGrid1.PageSize * DataGrid1.CurrentPageIndex
DataGridDataBind()
End Sub
解决方案 »
- fckeditor打开图片上传框的时候,变色白色
- 请问ASCX用户控件现在还推荐使用吗?还是大家都改用母版页?
- 江湖告急,treeview,任何控制
- Asp.Net和Vb.Net有什么区别阿?
- 如何在DataGrid里绑定数据,使得能够某列的一行可以显示个性化内容(比如是一个TABLE,里边有各种东西)
- 怎么使datagrid中第n行中一个textbox的获得焦点?
- 硬盘问题,有谁碰到过
- 关于路径的问题
- 熟悉xml操作的朋友。请帮我看看,不胜感激!!!在线等。。。。。。。。
- 请问在asp.net有没有类似于vb中Frame的控件?
- 用js怎样实现frameset的resize功能
- 问个属性的问题,一直不明白。(只有十分了,不知道为什么)
'编辑DataGrid1.EditItemIndex = e.Item.ItemIndex
Label1.Text = DataGrid1.CurrentPageIndex
btnSelect.Enabled = False
DataGridDataBind()
End Sub
============
1。编辑事件处理程序调用 DataGridDataBind() 的时候,startIndex 并未正确赋值,应该是你的初始值 0, 导致 da.Fill(ds, startIndex, DataGrid1.PageSize, "book") 从第一页开始加载数据》》》btnSelect.Enabled = False
startIndex = DataGrid1.PageSize * DataGrid1.CurrentPageIndex
DataGridDataBind()