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 /></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 + "&;


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

相关文章: