现在的位置: 首页 > 综合 > 正文

C# 生产新闻文章分页

2012年09月07日 ⁄ 综合 ⁄ 共 2824字 ⁄ 字号 评论关闭
有的时候我们遇到输出的内容太长,导致失去页面美观。现在我们采用给这个内容分页的方法来解决此问题:
代码:
<div id="mycontent" runat="server" style="z-index: 101; left: 100px; width: 621px; position: absolute; top: 40px;
            height: 160px"
> </div>
            
<div id="pager" runat="server" align="center" style="z-index: 101; left: 1px; width: 618px; position: absolute; top: 162px;
                height: 34px"
>
            
</div>

前台页面两个DIV用来显示信息,mycontent用来显示新闻内容,pager 用来显示分页信息

protected void Page_Load(object sender, EventArgs e)
    
{
        
int page=1;//初始页面代码,一打开显示第一页内容
        string mypage;
        mypage 
= Request.QueryString["page"];
        
if (mypage != null//第一次的时候没有加载分页因此得到的结果为NULL,因此要判断一下
        {
            page 
= Convert.ToInt32(mypage);
        }


        
if (!Page.IsPostBack)
        
{
            
          
//string strsql = "select top 1 * from pageContent where title='chen1'";
            string content = "2008年4月9日(星期三)上午10时,国务院新闻办新闻发布厅举行新闻发布会,<P>请西藏自治区主席向巴平措、中共中央统战部副部长斯塔介绍近期西藏有关情况,并答记者问。<P>以下为发布会部分内容摘要。向巴平措:前一段时间,我在慰问无辜死亡人员的亲属、到医院去看望受伤人员,<P>与受到损害的商户座谈的时候一再表示,作为政府,作为自治区政府主席,没有能够对纳税人、对人民群众很好地加以保护,因此我表示深深的歉意,<P>而且明确表示,作为人民政府,对死亡人员,对受伤人员,政府会通过多种渠道妥善解决他们受到的损失和面临的困难问题。";
            
//以上内容可以从数据库中取得,这里为方便演示直接写上内容,用<P>来间隔要取得的内容
          
            
string[] strContent = null;
           
// SqlDataReader dr = cdb.mydr(strsql); 如果从数据库里读的话
           
// if (dr.Read())
           
// {
                strContent = filesplit(content);//调用filesplit方法取得数组形式的内容
           
// }
           
// dr.Close();
            if (strContent[page - 1!= null)
            
{
                
                
this.mycontent.InnerHtml = strContent[page - 1].ToString();//显示内容
            }

            
else
            
{
                
this.mycontent.InnerHtml = "无内容";

            }

            
string adpager = string.Empty;
            
for (int i = 0; i < strContent.Length; i++)
            
{  //循环数组取得内容分页数
                if (strContent[i] != null)
                
{
                   
int npage = i + 1;
                    
                   adpager
+="<a href=Default.aspx?page=" + npage + ">" + npage + "</a>";
                  
                   
                }

            }

            
this.pager.InnerHtml = adpager.ToString();//显示分页

        }



    }



    
public string[] filesplit(string contents)
    
{   
        
int fileindex = 0;
        
string[] splitfile = new string[10];
        
while (contents.Length > 10 && fileindex < 9)
        
{
            
if (contents.IndexOf("<P>"10< 0break;

            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;
    }

此文方法引荐自http://blog.csdn.net/chenguang79/archive/2008/03/28/2224977.aspx

抱歉!评论已关闭.