您的位置:首页 > 编程语言 > ASP

asp.net新闻页面分页

2008-03-28 11:02 507 查看
如果一篇文章比较长,我们在一页面显示出来太长,不好看,想进行分页面。这在asp.net中如何处理呢。在网上查了一下资料,发现都是要在新闻添加 时加一下标记,但没有具体做法,后来有一个兄弟提出可以根据字数进行分。而且可以根据<P>这个标记来分。我就根据这个想法。写了下面的程 序,如果你有更好的方法,可以告诉我。给我留言。
因为一般情况下我们在发布新闻的时候,都是采用在线编辑器来处理,在线编辑器里断行都是大写的<P>下面是我的代码,没有美化。


private void Page_Load(object sender, System.EventArgs e)






{


// 在此处放置用户代码以初始化页面


if(!Page.IsPostBack)






{


int page = Convert.ToInt32(Request.QueryString["page"].ToString());


string strsql = "select top 1 * from pageContent where title='chen1'";




string[] strContent = null;


SqlDataReader dr = cdb.mydr(strsql);


if(dr.Read())






{


strContent = filesplit(dr["contents"].ToString());


}


dr.Close();


if(strContent[page-1]!=null)






{


Response.Write(strContent[page-1]);


}


else






{


Response.Write("为什么是空呢");


}


for(int i=0;i<strContent.Length;i++)






{


if(strContent[i]!=null)






{


int npage = i+1;


Response.Write("<a href=addView.aspx?page=" + npage + ">" + npage + "</a>");


}


}




}


}




public string[] filesplit(string contents)






{


int fileindex = 0;


string[] splitfile = new string[10];


while(contents.Length>10 && fileindex<9)






{


if(contents.IndexOf("<P>",10)<0) break;




splitfile[fileindex] = contents.Substring(0,contents.IndexOf("<P>",10));//这里注意这里的10是字数,我是为了测试而采用10,你可以根据你的新闻页面再设置,我想最少也得200字吧。呵呵。。。。。。


contents = contents.Remove(0,splitfile[fileindex].Length);


fileindex++;


}


splitfile[fileindex] = contents;


return splitfile;


}






Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码


override protected void OnInit(EventArgs e)






{


//


// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。


//


InitializeComponent();


base.OnInit(e);


}






/**//// <summary>


/// 设计器支持所需的方法 - 不要使用代码编辑器修改


/// 此方法的内容。


/// </summary>


private void InitializeComponent()






{


this.Load += new System.EventHandler(this.Page_Load);




}


#endregion


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: