用Asp.net實現新聞分頁
2024-07-10 12:57:33
供稿:網友
 
思想:方法是在后臺添加新聞時,估計在應該分頁的地方插入一個標記(我插入的是[page]),在前臺顯示時,我傳入了一個參數page,表示要顯示第幾頁,在顯示頁面,我根據標記[page]分別把新聞內容放入數組中,如果傳入的參數page的值是1,我就取arr[0]的值,如果是第二個......
還有,我把分頁的html代碼是根據統計的[page]來決定是否顯示的
baseinfo是我定義的基本信息類:
public class baseinfo
?{
??public string id,summary,content,picture;
??public datetime publictime;
??public stringbuilder pagefoot = new stringbuilder();
??public baseinfo()
??{?
???
??}
??//添加
??public int add()
??{
???...??
??}
??//更新
??public int update()
??{
???...
??}
??
??//初始化
??public void init()
??{
???...
??}
?}
在這個頁面要傳入兩個參數page和id,第一次鏈接到這個頁面傳入的page值應該是1,而且一定是1,id是
新聞的id,如新聞id=1的連接應該是baseinfodetail.aspx?page=1&id=1
nextat是后一個[page]的位置,backat是前一個[page]標志的位置,pagefoot是一個html表格我加了
runat=server
代碼如下:
baseinfodetail.aspx的部分代碼如下:
baseinfo info = new baseinfo();??
????info.id = request.querystring["id"];?
????info.init();
????
//******************************************************************************************
**********
????//分頁
????// i:計算標志“[page]”的個數的
????//
????//???????????????? --? 夢凡? 2004年8月14號19:32
????int nextat = -4,i=0,backat = 0;
????string[] tempcontent = new string[20] ;
????do 
????{
?????nextat += 4;
?????nextat = info.content.indexof("[page]",nextat);??
????????????
?
?????if(nextat != -1)
?????{
??????i++;
??????tempcontent[i] = info.content.substring
(backat,math.abs(nextat-backat));
??????backat = nextat + 6;
?????}
????}while(nextat != -1);
??????
????int currentpage = convert.toint32(request.querystring
["page"]);
????if(i>0)
????{
?????info.content = tempcontent[currentpage];
?????this.pagefoot.visible = true;
????}
????else
?????this.pagefoot.visible = false;
?????
????info.pagefoot.append("共" +i.tostring()+"頁 ");
????for(int j=1;j<=i;j++)
????{
?????info.pagefoot.append("[ 
href='baseinfodetail.aspx?page=" +j.tostring()+ "&id=" +info.id+ "'>" +j.tostring()+ "]
 ");
????}
????if(currentpage != i)
????{
?????currentpage += 1;
?????info.pagefoot.append("下一頁 ");
????}
????
????
//******************************************************************************************
**********
網站運營seo文章大全提供全面的站長運營經驗及seo技術!