以下是小弟摘出的代码。想实现分页处理。用datagrid显示数据。但是当页面处于page1时候,点next(下一页)到page2时候,需要点2下,同理,页面处于last时候,点prev(上一页)也是需要两下。其他的没问题。
小弟找的实在喷火了~~>_<~~希望各位有同样经历的来顶,高手指点江山。
html的代码如下。<asp:datagrid Width="100%" runat="server" id="dgout" AllowSorting="True" OnSortCommand="dgsortpage"
headerstyle-backcolor="#aaaadd" BorderWidth="0" alternatingitemstyle-backcolor="#eeeeee" bordercolor="black"
cellpadding="2" cellspacing="0" allowpaging="true" pagerstyle-mode="numericpages" pagesize="3"
onpageindexchanged="changepage" pagerstyle-horizontalalign="right" autogeneratecolumns="false">
<columns>
<asp:boundcolumn datafield="序号" ItemStyle-Width="3.5%" headertext="序号" HeaderStyle-Wrap="False" itemstyle-horizontalalign="right" />
<asp:hyperlinkcolumn datatextfield="产品名称" headertext="产品名称" HeaderStyle-Wrap="False" datanavigateurlfield="产品名称"
datanavigateurlformatstring="students.aspx?id=(0)" target="_blank" />
<asp:boundcolumn datafield="发布时间" headertext="发布时间" HeaderStyle-Wrap="False" />
<asp:boundcolumn datafield="生产企业" headertext="生产企业" HeaderStyle-Wrap="False" />
<asp:boundcolumn datafield="单位" headertext="单位" HeaderStyle-Wrap="False" />
<asp:boundcolumn datafield="市场售价" headertext="市场售价" itemstyle-horizontalalign="right" HeaderStyle-Wrap="False"
itemstyle-width="8%" />
</columns>
</asp:datagrid>
vb代码如下。:
Public Sub oleconn() Dim constr As String constr = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=cbcp;Data Source=CSSC-GL;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=CSSC-GL;Use Encryption for Data=False;Tag with column collation when possible=False"
conn = New OleDbConnection(constr)
conn.Open()
End Sub
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnFirst.Text = "最首页"
btnPrev.Text = "前一页"
btnNext.Text = "下一页"
btnLast.Text = "最后页" If Not IsPostBack Then
'-------------------------------------------清空查询栏目---------
'if ispostback then txtfind.text="" oleconn()
bindgrid() End If
End Sub
Sub changepage(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
dgout.CurrentPageIndex = e.NewPageIndex
oleconn()
bindgrid()
ShowStats()
End Sub
Private Sub bindgrid()
Dim adpt As OleDbDataAdapter
Dim ds As DataSet
Dim consql As String
consql = "select * from cbcpdb"
adpt = New OleDbDataAdapter(consql, conn) ds = New DataSet
adpt.Fill(ds, "cbcpdb")
ds.Tables("cbcpdb").DefaultView.Sort = sortfield.SelectedItem.Text & sorttype.Text dgout.DataSource = ds.Tables("cbcpdb").DefaultView
dgout.DataBind() conn.Close() End Sub
Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
'used by external paging UI
Dim arg As String = sender.CommandArgument
On Error Resume Next Select Case arg
Case "next"
If (dgout.CurrentPageIndex < (dgout.PageCount - 1)) Then
dgout.CurrentPageIndex += 1
End If
Case "prev"
If (dgout.CurrentPageIndex > 0) Then
dgout.CurrentPageIndex -= 1
End If
Case "last"
dgout.CurrentPageIndex = (dgout.PageCount - 1)
Case Else
'page number
dgout.CurrentPageIndex = System.Convert.ToInt32(arg)
End Select
bindgrid()
ShowStats()
End Sub
Private Sub ShowStats()
lblCurrentIndex.Text = "您现在看的是第 " + (dgout.CurrentPageIndex + 1).ToString() + " 页"
lblPageCount.Text = "当前总共 " + dgout.PageCount.ToString() + " 页"
End Sub
Sub subsort_click(ByVal sender As Object, ByVal e As EventArgs)
oleconn()
End Sub
小弟找的实在喷火了~~>_<~~希望各位有同样经历的来顶,高手指点江山。
html的代码如下。<asp:datagrid Width="100%" runat="server" id="dgout" AllowSorting="True" OnSortCommand="dgsortpage"
headerstyle-backcolor="#aaaadd" BorderWidth="0" alternatingitemstyle-backcolor="#eeeeee" bordercolor="black"
cellpadding="2" cellspacing="0" allowpaging="true" pagerstyle-mode="numericpages" pagesize="3"
onpageindexchanged="changepage" pagerstyle-horizontalalign="right" autogeneratecolumns="false">
<columns>
<asp:boundcolumn datafield="序号" ItemStyle-Width="3.5%" headertext="序号" HeaderStyle-Wrap="False" itemstyle-horizontalalign="right" />
<asp:hyperlinkcolumn datatextfield="产品名称" headertext="产品名称" HeaderStyle-Wrap="False" datanavigateurlfield="产品名称"
datanavigateurlformatstring="students.aspx?id=(0)" target="_blank" />
<asp:boundcolumn datafield="发布时间" headertext="发布时间" HeaderStyle-Wrap="False" />
<asp:boundcolumn datafield="生产企业" headertext="生产企业" HeaderStyle-Wrap="False" />
<asp:boundcolumn datafield="单位" headertext="单位" HeaderStyle-Wrap="False" />
<asp:boundcolumn datafield="市场售价" headertext="市场售价" itemstyle-horizontalalign="right" HeaderStyle-Wrap="False"
itemstyle-width="8%" />
</columns>
</asp:datagrid>
vb代码如下。:
Public Sub oleconn() Dim constr As String constr = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=cbcp;Data Source=CSSC-GL;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=CSSC-GL;Use Encryption for Data=False;Tag with column collation when possible=False"
conn = New OleDbConnection(constr)
conn.Open()
End Sub
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnFirst.Text = "最首页"
btnPrev.Text = "前一页"
btnNext.Text = "下一页"
btnLast.Text = "最后页" If Not IsPostBack Then
'-------------------------------------------清空查询栏目---------
'if ispostback then txtfind.text="" oleconn()
bindgrid() End If
End Sub
Sub changepage(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
dgout.CurrentPageIndex = e.NewPageIndex
oleconn()
bindgrid()
ShowStats()
End Sub
Private Sub bindgrid()
Dim adpt As OleDbDataAdapter
Dim ds As DataSet
Dim consql As String
consql = "select * from cbcpdb"
adpt = New OleDbDataAdapter(consql, conn) ds = New DataSet
adpt.Fill(ds, "cbcpdb")
ds.Tables("cbcpdb").DefaultView.Sort = sortfield.SelectedItem.Text & sorttype.Text dgout.DataSource = ds.Tables("cbcpdb").DefaultView
dgout.DataBind() conn.Close() End Sub
Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
'used by external paging UI
Dim arg As String = sender.CommandArgument
On Error Resume Next Select Case arg
Case "next"
If (dgout.CurrentPageIndex < (dgout.PageCount - 1)) Then
dgout.CurrentPageIndex += 1
End If
Case "prev"
If (dgout.CurrentPageIndex > 0) Then
dgout.CurrentPageIndex -= 1
End If
Case "last"
dgout.CurrentPageIndex = (dgout.PageCount - 1)
Case Else
'page number
dgout.CurrentPageIndex = System.Convert.ToInt32(arg)
End Select
bindgrid()
ShowStats()
End Sub
Private Sub ShowStats()
lblCurrentIndex.Text = "您现在看的是第 " + (dgout.CurrentPageIndex + 1).ToString() + " 页"
lblPageCount.Text = "当前总共 " + dgout.PageCount.ToString() + " 页"
End Sub
Sub subsort_click(ByVal sender As Object, ByVal e As EventArgs)
oleconn()
End Sub
解决方案 »
- 各位大侠,大家在做b/s模式系统的,用什么第三方控件美化界面呀
- C#中的“~”是什么意思????
- 控件问题
- 求静态网页生成思路?希望牛人能给个思路?
- .net中如何在框架中传递参数
- 请问ActiveX空间在客户端运行后它本身的一些数据改变了,服务器端怎么调用这个控件的方法获得该控件的数据?
- 神啊救救我吧!(ASP.NET)
- 请问怎么在javascript里使用timer
- 在ASP.net中无法使用MessageBox吗?
- 怎样用c#,asp.net写:新开一个新窗口,新窗口中有日历,在日历单击事件中,关闭传递日期,关闭窗口?
- 请教一个rowfilter语句的写法。(马上结贴,在线等)
- detailspage.aspx?id={0}" 如何生成detailspage.aspx?id={0}这个页面这个页面该怎么写.
你把click 事件的代码放到page load 里 应该就可以了我以前也遇到过这样的问题 但是我在changepage里面重新DataBind后 问题就消失了
不知道你为什么还是这样
关注中