惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - flyfish

Microsoft SQL Server 2005 -- 错误 29503。SQL Server 服务无法启动 模拟鼠标移动和左键单击 .C# 获取另一程序控件,改变值,触发事件 - flyfish - 博客园 在ASP程序中调用Web Service ASPNET2.0中读写Cookie的方法! - flyfish - 博客园 转 跨域读取Cookie和session之HttpWebRequest另类方法(网站API开发) ASP.NET WAP开发 用ASP.NET创建移动Web窗体[转] ASP.NET 2.0移动开发入门之使用模拟器 SQL Server 自增字段归零等问题 ntext replace sql SQLServer2005数据库还原到SQLServer2000 如何去除Google搜索结果病毒提示 Tomcat 6 连接 MS SQL 2005 Windows 2003远程桌面连接数限制 如何用SQL命令修改字段名称 FCKeditor详细的设置 log4net 配置与应用 两个sql server 2000的通用分页存储过程
[WAP]dotNet在WAP应用开发中实现按指定页数翻页的解决方案
flyfish · 2009-06-26 · via 博客园 - flyfish

PanQi
Ultrapower Software

简介:采用dotNet开发WAP应用,通常会遇到翻页的问题,在dotNetList控件中虽然提供ItemCountItemsPerPage属性来实现翻页,但这种自带的翻页只能实现上一页下一页的翻页效果,无法按指定页数来选择性翻页,因此,很有必要开发自己的翻页机制,来完善它的不足之处,本文档将一步一步介绍如何用.Net提供有限的MobileControl来实现按指定页数翻页。

如您要转贴,请保留原出处,并勿做删改。谢谢。

源码下载>>

方案一:采用数据绑定,指定数据源(DataTable)实现翻页的方法

第一步,打开VS.Net,新建Asp.Net移动Web应用,我们这里将项目命名为WapPager,如下图:

第二步,向

Form里拖入所需控件:

Label控件,一个,用于显示提示消息。

List控件,一个,用于绑定数据,显示指定范围内的记录。

Panel控件,一个,用于存放表达式计算出的n个页面的Link页码。

页面布局如下图所示:

第三步,修改控件属性,为编码作准备(具体属性设置请看Demo)。

第四步,编码。(说明:演示数据来自SQL-ServerNorthWind数据库)

为了实现翻页效果,需要用到下面三个方法,分别是:

1、 GetAllData()方法:

GetAllData是获取全部数据,返回DataTable的方法,之所以采用DataTable来存放全部数据,主要是为了后面用DataTableSelect方法来获取其中指定范围内记录

private DataTable GetAllData()
{
    
this.sqlDataAdapter1.Fill(this.dataSet11);
    
return this.dataSet11.Tables["Products"];
}


2、 GetPage()方法:

GetPage是通过数据总数、每页规定显示数量,计算出页数的方法,返回存放全部页码的DataTable,我们为这个DataTable定义了LinkText和LinkUrl两列,这两列的值供页码控件Link绑定数据所用。

private DataTable GetPage() 

    DataTable dtPage 
= new DataTable(); 
    DataTable dtAllData 
= this._dtAllData; 
    
int dataCount = dtAllData.Rows.Count; 
    
int    pageCount = 0
    
if((dataCount%GlobalConfig.Size) == 0
    

        pageCount 
= dataCount/GlobalConfig.Size; 
    }
 
    
if((dataCount%GlobalConfig.Size) != 0
    

        pageCount 
= dataCount/GlobalConfig.Size + 1
    }
 
    dtPage.Columns.Add(
"LinkText",typeof(String)); 
    dtPage.Columns.Add(
"LinkUrl",typeof(String)); 
    
for(int i = 0;i < pageCount; i++
    

        DataRow row 
= dtPage.NewRow(); 
        row[
"LinkText"= i + 1
        
if(i == 0
        

            row[
"LinkUrl"= "MobileWebForm1.aspx?startID=" + ((i*GlobalConfig.Size)) + "&selectPage=" + i;  
        }
 
        
else 
        

            row[
"LinkUrl"= "MobileWebForm1.aspx?startID=" + ((i*GlobalConfig.Size) + 1+ "&selectPage=" + i;  
        }
 
        dtPage.Rows.Add(row); 
    }
 
    
return dtPage; 
}
 
 


3、 SelectTable()方法:

SelectTable方法是利用DataTable自身的Select方法,传入起始值,读取指定行数记录,最终实现翻页效果.

private void SelectTable(int startID) 

    
try 
    

        DataTable dtAllData        
= this._dtAllData; 
        DataTable dtSelectData    
= new DataTable(); 
        dtSelectData.Columns.Add(
"ProductID",typeof(String)); 
        dtSelectData.Columns.Add(
"ProductName",typeof(String)); 
        
string strExpr; 
        
int endID = startID + GlobalConfig.Size; 
        
if(startID == 0
        

            strExpr 
= "ProductID >= "+ startID +" and ProductID <= "+ endID +""
        }
 
        
else 
        

            strExpr 
= "ProductID >= "+ startID +" and ProductID < "+ endID +""
        }
 
        
if(dtAllData != null && dtAllData.Rows.Count > 0
        

            DataRow[] foundRows 
= dtAllData.Select(strExpr); 
            
string flag1 = foundRows.Length.ToString(); 
            
for(int i = 0; i < foundRows.Length; i ++
            

                DataRow row             
= dtSelectData.NewRow(); 
                row[
"ProductID"]     = foundRows[i][0]; 
                row[
"ProductName"]     = foundRows[i][1]; 
                dtSelectData.Rows.Add(row); 
            }
 
            
if(dtSelectData != null && dtSelectData.Rows.Count > 0
            

                
this.List.DataSource = dtSelectData; 
                
this.List.DataBind(); 
            }
 
            
try 
            

                DataTable dtPage 
= GetPage(); 
                
string selectPage = Page.Request["SelectPage"]; 
                
for(int i = 0; i < dtPage.Rows.Count; i++
                

                    Link link 
= new System.Web.UI.MobileControls.Link(); 
                    
if(i == Convert.ToInt32(selectPage)) 
                    

                        link.Text 
= "[本页" + dtPage.Rows[i][0].ToString() + "]"
                    }
 
                    
else 
                    

                        link.Text 
= "[" + dtPage.Rows[i][0].ToString() + "]"
                    }
 
                    link.NavigateUrl 
= dtPage.Rows[i][1].ToString(); 
                    link.ID 
= i.ToString(); 
                    link.BreakAfter 
= false
                    
this.Panel.Controls.Add(link); 
                }
 
            }
 
            
catch(Exception exc) 
            

                
this.lblMessage.Text = exc.Message; 
                
this.lblMessage.Visible = true
            }
 
        }
 
        
else 
        

            
this.lblMessage.Text = "没有记录!"
            
this.lblMessage.Visible = true
        }
 
    }
 
    
catch(Exception exc) 
    

        
this.lblMessage.Text = exc.Message; 
        
this.lblMessage.Visible = true
    }
 
}
 


页面最终效果:  
 

注:每页显示最多记录条数可以在Web.Config的GlobalSection属性Record.Page.Size节点自行定义,默认为每页显示10条记录。

 如果您有任何意见或建议,请联系作者:Email:PanQi7000(a)126.com请将(a)替换为@。

Write by MyXQ