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

.net 新闻内容分页

2012年11月11日 ⁄ 综合 ⁄ 共 7830字 ⁄ 字号 评论关闭

html:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="shownews.aspx.cs" Inherits="shownews" %>

<%@ Register Src="headControl.ascx" TagName="headControl" TagPrefix="uc1" %>
<%@ Register Src="buttonControl.ascx" TagName="buttonControl" TagPrefix="uc2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title><%=title %></title>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table align="center" border="0" cellpadding="0" cellspacing="0" style="width: 1000px">
            <tr>
                <td >
                    <uc1:headControl ID="HeadControl1" runat="server" />                </td>
            </tr>
            <tr>
                            <td class="newslist" >
                                您现在的位置&gt;<a href="index.aspx" target="_self">首页</a>&gt;
              <asp:HyperLink ID="hl_link"
                                    runat="server">[HyperLink1]</asp:HyperLink></td>
          </tr>
          <tr><td>
              <table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
                  <tr>
                      <td  valign="top" class="newbakcolor">
                          <table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
                              <tr>
                                  <td class="newtitle" >
                              
                          <asp:Label ID="lb_title" runat="server"></asp:Label></td>
                              </tr>
                              <tr>
                                  <td align="center" class="newinfo" >
                          <asp:Label ID="lb_newinfo" runat="server"></asp:Label></td>
                              </tr>
                              <tr>
                                  <td class="newcontent" >
                          <div id="newcontent" runat="server">
                              &nbsp;</div></td>
                              </tr>
                              <tr>
                                  <td class="newcontent">
                                      <asp:Panel ID="Panel1" runat="server" Visible="False" Width="100%"><strong><div id="pageinfo" runat="server" align=center> </div></strong>
                                      </asp:Panel>
                                
                                  </td>
                              </tr>
                        </table>
                    </td>
                      <td width="300" align="right" valign="top">
                          <table border="0" cellpadding="0" cellspacing="0" class="newtdcolor" style="width: 98%">
                              <tr>
                                  <td align="left" class="newlistbg" >最近更新                                  </td>
                              </tr>
                              <tr>
                                  <td align=left >
                                <asp:DataList ID="DataList1" runat="server" Width="100%">
                                    <ItemTemplate>
                                       <table width="100%" border="0" cellspacing="0" cellpadding="0">
         <tr valign="middle">
          <td width="7" height="24" align="left">
           <img src="images/index-dot1.jpg" border="0"></td>
          <td><a href='shownews.aspx?id=<%#DataBinder.Eval(Container.DataItem,"Ns_Id")%>' title='<%#DataBinder.Eval(Container.DataItem,"Ns_Subject") %>'><%#DataBinder.Eval(Container.DataItem, "Ns_Subject")%></a></td>
        
        
        </table>
                                    </ItemTemplate>
                                </asp:DataList></td>
                              </tr>
                        </table>
                    </td>
                  </tr>
            </table>
          </td></tr>
            <tr>
                 <td><uc2:buttonControl ID="ButtonControl1" runat="server" /></td>
            </tr>
      </table>
   
      
    </div>
        &nbsp; &nbsp;
    </form>
</body>
</html>
c#:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class shownews : System.Web.UI.Page
{
    public string title;
    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 (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                string id, str;
                id = Request.QueryString["id"].Trim();
                str = "update news set Ns_Click=Ns_Click+1 where Ns_Id=" + id;
                db.executesql(str, "");
                str = "select * from news where Ns_Id=" + id;
                SqlDataReader sdr = db.getsdr(str);
                if (sdr.Read())
                {
                    //this.lb_content.Text = sdr["Ns_Content"].ToString();
                    this.lb_title.Text = sdr["Ns_Subject"].ToString();
                    title = sdr["ns_subject"].ToString().Trim() + "_" + sdr["Ns_type"].ToString();
                    this.hl_link.Text = sdr["Ns_type"].ToString();
                    db.binddatalist("select top 8 * from news where Ns_type='" + sdr["Ns_type"].ToString().Trim() + "' order by ns_id desc", this.DataList1, "ns_id");
                    this.hl_link.NavigateUrl = "newslist.aspx?type=" + sdr["Ns_type"].ToString().Trim();
                    this.lb_newinfo.Text = "作者:" + sdr["Ns_Author"].ToString().Trim() + " 点击数:" + sdr["Ns_Click"].ToString().Trim() + " 更新时间:" + sdr["Ns_date"].ToString();
                    //this.newcontent.InnerHtml = "kfdfsdasddfdskjdfdkfjdfsj";
                    //this.pageinfo.InnerHtml = "kdkfjdkf";
                    string content;
                    content = sdr["Ns_Content"].ToString();
                    string[] strContent = null;
                   
                    strContent = filesplit(content);//调用filesplit方法取得数组形式的内容
                 
                    if (strContent[page - 1] != null)
                    {

                        this.newcontent.InnerHtml = strContent[page - 1].ToString();//显示内容
                    }
                    else
                    {
                        this.newcontent.InnerHtml = "无内容";

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

                        }
                    }
                    if (npage > 1)
                    {
                        this.Panel1.Visible = true;
                    }
                    this.pageinfo.InnerHtml = "分页信息"+adpager.ToString();//显示分页
                  
                }
                db.closeall(null, null, sdr);
            }
            else
            {
                db.msgback("参数输入错误!", "index.aspx");
            }
        }

    }
    public string[] filesplit(string contents)
    {
        int fileindex = 0;
        string[] splitfile = new string[10];
        while (contents.Length > 10 && fileindex < 9)
        {
            string tt = @"<div style=""page-break-after: always""><span style=""display: none"">&nbsp;</span></div>";
            if (contents.IndexOf(tt, 10) < 0) break;

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

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wrzme/archive/2008/12/08/3475133.aspx

抱歉!评论已关闭.