采育镇政府网:ASP中关于新闻内容过长,需要分页怎么做

来源:百度文库 编辑:高校问答 时间:2024/04/28 05:11:13
在新闻系统,一单条新闻内容过长,怎么样能把新闻内容分成多页显示?

本人用c#加asp.net做了一个新闻分页,相当简单。
一:在网页页面上放两个label控件
二:在后台代码里这样写:
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
shownews(Label1,Label2,"关于asp的分页",4);//label1主要是显示新闻,label2是分页的那个页码,引号中的是新闻,4是一页显示的多少个字,也就是每4个字分一页
}
}

private void shownews(Label lb1,Label lb2,string newscontent,int divnum)
{
int currpage=0;
int totalpages=((newscontent.Length%divnum==0)?(newscontent.Length/divnum):(newscontent.Length/divnum+1));
if(Request.QueryString["page"]!=null)
currpage=Convert.ToInt32(Request.QueryString["page"]);
else
currpage=1;
if(currpage==totalpages)
lb1.Text=newscontent.Substring((currpage-1)*divnum);
else
lb1.Text=(newscontent.Length>=divnum)?newscontent.Substring((currpage-1)*divnum,divnum):newscontent.Substring(0);
for(int i=1;i<=totalpages;i++)
lb2.Text+="<a href="+Request.CurrentExecutionFilePath+"?page="+i.ToString()+">"+i.ToString()+"  </a>";
}