用magicajax实现无刷新分页

现在Ajax的应用越来越广,它的好处在这里就不多说了。直接进入主题:
现在这方面技术主要有三种:
    1: Ajaxpro.net  2:   Atlas    3:   MagicAjax
    其中Atlas只能用于2.0下,另外两种适合于1.1   2.0 两种版本的都有。
下面的例子是在VS2003,.NET1.1下测试通过。

至于magicajax.dll的使用方法,我大至说一下:

1:当然先引入magicajax.dll  

2:在 webconfig增加下面的配置:

 <configSections>
      <section name="magicAjax"  type="MagicAjax.Configuration.MagicAjaxSectionHandler, MagicAjax"/>
    </configSections>
    <magicAjax outputCompareMode="HashCode" tracing="false" >
        <pageStore mode="NoStore"
   unloadStoredPage="false"
   cacheTimeout="5"
   maxConcurrentPages="5"
   maxPagesLimitAlert="false" />
    </magicAjax>
  <system.web>

<httpModules>
           <add name="MagicAjax" type="MagicAjax.MagicAjaxModule, MagicAjax" />
   </httpModules>
。。。。。。

。。。。。省略

 </system.web>

我们现在用一个repeater控件来做为一个显示数据的表格。还是先看页面的代码:

<% @ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="test_datagrid.WebForm1" %>
<% @ Register TagPrefix="ajax" Namespace="MagicAjax.UI.Controls" Assembly="MagicAjax"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
    
< HEAD >
        
< title > WebForm1 </ title >
        
< meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
        
< meta  content ="Visual Basic .NET 7.1"  name ="CODE_LANGUAGE" >
        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
    
</ HEAD >
    
< body  MS_POSITIONING ="GridLayout" >
        
< form  id ="Form1"  method ="post"  runat ="server" >
            
< FONT  face ="宋体" > 这是一个简单的用magicajax来实现无刷新分页的例子:(不足的地方,请大家见谅!) < br >
            
</ FONT >
            
< ajax:ajaxpanel  id ="AjaxPanel1"  runat ="server" >
                
< asp:Repeater  id ="Repeater1"  runat ="server" >
                    
< HeaderTemplate >
                        
< table  id ="table1"  Width ="100%"  cellspacing ="0"  cellSpacing ="1"  border ="1"  bordercolor ="Gainsboro" >
                            
< tr  class ="tr_head"  align ="Center"  valign ="Middle" >
                                
< td  Width ="48" > id </ td >
                                
< td > text </ td >
                            
</ tr >
                    
</ HeaderTemplate >
                    
< AlternatingItemTemplate >
                        
< tr  align ="Center"  valign ="Middle" >
                            
< td  align ="Left" > <% # DataBinder.Eval(Container, "DataItem.id" %> </ td >
                            
< td  align ="Left" > <% # DataBinder.Eval(Container, "DataItem.text" %> </ td >
                        
</ tr >
                    
</ AlternatingItemTemplate >
                    
< ItemTemplate >
                        
< tr  align ="Center"  valign ="Middle" >
                            
< td  align ="Left" > <% # DataBinder.Eval(Container, "DataItem.id" %> </ td >
                            
< td  align ="Left" > <% # DataBinder.Eval(Container, "DataItem.text" %> </ td >
                        
</ tr >
                    
</ ItemTemplate >
                    
< FooterTemplate >
                        
</ TABLE >
                    
</ FooterTemplate >
                
</ asp:Repeater >
            
</ ajax:ajaxpanel >< ajax:ajaxpanel  id ="Ajaxpanel2"  runat ="server" >
                
< TABLE  cellSpacing ="4"  cellPadding ="0"  width ="100%"  border ="0" >
                    
< TR >
                        
< TD  width ="*" >< SPAN  id ="pager1_l_page" >< FONT  color ="red" >
                                    
< asp:label  id ="nowpage"  Runat ="server"  Visible ="False"  ForeColor ="red"  Width ="0px" ></ asp:label ></ FONT >
                                
< asp:label  id ="totalpage"  Runat ="server"  Visible ="False"  Width ="0px" ></ asp:label >
                                
< asp:label  id ="totalrecord"  Runat ="server"  Visible ="False"  Width ="0px" ></ asp:label >
                                
< asp:label  id ="L_page"  runat ="server" ></ asp:label ></ SPAN ></ TD >
                        
< TD  vAlign ="middle"  align ="right"  width ="400" >
                            
< asp:linkbutton  id ="linkf"  title ="首页"  runat ="server"  CssClass ="btncol" > 首页 </ asp:linkbutton >
                            
< asp:linkbutton  id ="Linkp"  title ="上一页"  runat ="server"  CssClass ="btncol" > 上一页 </ asp:linkbutton >
                            
< asp:linkbutton  id ="Linkn"  title ="下一页"  runat ="server"  CssClass ="btncol" > 下一页 </ asp:linkbutton >
                            
< asp:linkbutton  id ="Linkl"  title ="未页"  runat ="server"  CssClass ="btncol" > 未页 </ asp:linkbutton > &nbsp; 跳转:
                            
< asp:textbox  id ="go1"  Runat ="server"  Width ="29px" ></ asp:textbox >< asp:LinkButton  id ="Lgo"  runat ="server" > GO </ asp:LinkButton ></ TD >
                    
</ TR >
                
</ TABLE >
            
</ ajax:ajaxpanel ></ form >
    
</ body >
</ HTML >

上面的代码没有什么好讲的,想必大家看一下就都懂的。主要的是后台的代码:

 

Imports  System.Data.OleDb
Imports  System.Data

Public   Class WebForm1
    
Inherits System.Web.UI.Page
    
Dim connection As OleDbConnection
    
Const TNAME = "test"
    
Const dbname = "testmdb.mdb"
    
Protected WithEvents AjaxPanel1 As MagicAjax.UI.Controls.AjaxPanel
    
Protected WithEvents Repeater1 As System.Web.UI.WebControls.Repeater
    
Protected WithEvents nowpage As System.Web.UI.WebControls.Label
    
Protected WithEvents totalpage As System.Web.UI.WebControls.Label
    
Protected WithEvents totalrecord As System.Web.UI.WebControls.Label
    
Protected WithEvents L_page As System.Web.UI.WebControls.Label
    
Protected WithEvents linkf As System.Web.UI.WebControls.LinkButton
    
Protected WithEvents Linkp As System.Web.UI.WebControls.LinkButton
    
Protected WithEvents Linkn As System.Web.UI.WebControls.LinkButton
    
Protected WithEvents Linkl As System.Web.UI.WebControls.LinkButton
    
Protected WithEvents go1 As System.Web.UI.WebControls.TextBox
    
Protected WithEvents Lgo As System.Web.UI.WebControls.LinkButton
    
Protected WithEvents Ajaxpanel2 As MagicAjax.UI.Controls.AjaxPanel
    
Dim _PageCount As Integer
    
Dim _TotalCount As Integer


Web 窗体设计器生成的代码

    
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
'在此处放置初始化页的用户代码
       
        BindRepeater(
51)
        nowpage.Text 
= 1

        pageset()


    
End Sub

#Region "有关数据库连接与操作 "
    
Private Sub conn()
        
'新建一个连接
        Dim ConnectString As String
        ConnectString 
= "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" & Server.MapPath(dbname)  '连接ORACLE的字符串
        connection = New OleDbConnection(ConnectString) '新建一个连接

    
End Sub

    
Private Sub openconn()
        
'打开连接
        connection.Open()
    
End Sub

    
Private Sub closeconn()
        
'关闭连接
        connection.Close()
    
End Sub

    
Private Sub BindRepeater(ByVal pagesize As IntegerByVal currentpage As Integer)
        
Dim sql As String
        sql 
= "select id,text from " & TNAME
        conn()
        openconn()
        Repeater1.DataSource 
= getdatasource(sql, pagesize, currentpage)
        Repeater1.DataBind()
        closeconn()

    
End Sub

    
Private Function getdatasource(ByVal sql As StringByVal pagesize As IntegerByVal curentpage As Integer)
        
'用来获得要显示数据的视图
        Dim ds As DataSet
        
Dim objPds As PagedDataSource
        
Dim adapter As OleDbDataAdapter
        ds 
= New DataSet
        objPds 
= New PagedDataSource
        adapter 
= New OleDbDataAdapter(sql, connection)
        adapter.Fill(ds, 
"Table")
        objPds.DataSource 
= ds.Tables(0).DefaultView()
        objPds.AllowPaging 
= True
        objPds.PageSize 
= pagesize
        _PageCount 
= objPds.PageCount
        _TotalCount 
= ds.Tables(0).DefaultView().Count
        objPds.CurrentPageIndex 
= curentpage
        
Return objPds
    
End Function


#End Region

#Region "翻页"
    
Private Sub pageset()
        
If nowpage.Text = "1" Then
            linkf.Enabled 
= False
            Linkp.Enabled 
= False
        
Else
            linkf.Enabled 
= True
            Linkp.Enabled 
= True
        
End If
        
If nowpage.Text = _PageCount Then
            Linkl.Enabled 
= False
            Linkn.Enabled 
= False
        
Else
            Linkl.Enabled 
= True
            Linkn.Enabled 
= True
        
End If
        totalpage.Text 
= _PageCount
        totalrecord.Text 
= _TotalCount
        
If _TotalCount = 0 Then
            nowpage.Text 
= "0"
        
End If
        L_page.Text 
= "第 <FONT color='red'>" + nowpage.Text + "</font> 页/共 " + totalpage.Text + " 页,总计 " + totalrecord.Text + " 条"
    
End Sub

    
Private Sub linkf_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles linkf.Click
        Session(
"curent"= 0
        BindRepeater(
5, Session("curent"))
        Linkp.Enabled 
= False
        nowpage.Text 
= "1"
        pageset()
    
End Sub

    
Private Sub Linkl_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Linkl.Click
        Session(
"curent"= _PageCount - 1
        BindRepeater(
5, Session("curent"))
        Linkn.Enabled 
= False
        nowpage.Text 
= Session("curent"+ 1
        pageset()
    
End Sub

    
Private Sub Linkn_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Linkn.Click
        Session(
"curent"= Session("curent"+ 1
        BindRepeater(
5, Session("curent"))
        nowpage.Text 
= Session("curent"+ 1
        pageset()
    
End Sub

    
Private Sub Linkp_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Linkp.Click
        Session(
"curent"= Session("curent"- 1
        BindRepeater(
5, Session("curent"))
        nowpage.Text 
= Session("curent"+ 1
        pageset()
    
End Sub

    
Private Sub Lgo_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Lgo.Click
        
Dim go As Integer
        
If IsNumeric(Trim(go1.Text)) Then
            
If (Int(Trim(go1.Text)) > 0And (Int(Trim(go1.Text)) < _PageCount) Then
                go 
= Int(Trim(go1.Text))
                Session(
"curent"= go - 1
                BindRepeater(
5, Session("curent"))
                nowpage.Text 
= Session("curent"+ 1
                pageset()
            
End If
        
End If
    
End Sub
 
#End Region

End Class

 我是用PageDateSource来做为数据源,呵原因当然是这个更方便应用于无刷新的分页。

文章的主要思想:是用session来存储显示的页面。用PageDateSource来做为数据源

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值