using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
//using System.Configuration;
namespace AboutMe.Document
{
/// <summary>
/// Show 的摘要说明。
/// </summary>
public class Show : System.Web.UI.Page
{
protected DataTable DocumentTable=null;
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
protected System.Web.UI.WebControls.Label labCount;
protected System.Web.UI.WebControls.Label labTop;
protected System.Web.UI.WebControls.Label labNext;
protected System.Web.UI.WebControls.TextBox txtID;
protected System.Web.UI.WebControls.Button butGo;
protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator2;
protected System.Web.UI.WebControls.Label labCount1;
protected System.Web.UI.WebControls.Label labTop1;
protected System.Web.UI.WebControls.Label labNext1;
protected System.Web.UI.WebControls.TextBox txtID1;
protected System.Web.UI.WebControls.Button butGo1;
protected System.Web.UI.WebControls.Label labError;
protected System.Web.UI.WebControls.Label labError1;
protected int id=0;
protected int Count=0;
private void Page_Load(object sender, System.EventArgs e)
{
id=Convert.ToInt16(Page.Request["id"].ToString());
GetData(id);//显示数据
NewData(id);//更新游览次数
RowCount();
ShowPage(id);//显示标头
}
private void ShowPage(int id)//显示标头
{
if(id==0)
{
id=1;
}
if(id>Count)
{
id=Count;
}
labCount.Text=id.ToString()+"/"+Count.ToString();
labCount1.Text=id.ToString()+"/"+Count.ToString();
if(id==1)
{
labNext.Text="<a href='show.aspx?id="+(id+1)+"'>下一篇</a>";
labNext1.Text="<a href='show.aspx?id="+(id+1)+"'>下一篇</a>";
return;
}

if (id==Count)
{
labTop.Text="<a href='show.aspx?id="+(id-1)+"'>上一篇</a>";
labTop1.Text="<a href='show.aspx?id="+(id-1)+"'>上一篇</a>";
return;
}

labNext.Text="<a href='show.aspx?id="+(id+1)+"'>下一篇</a>";
labNext1.Text="<a href='show.aspx?id="+(id+1)+"'>下一篇</a>";
labTop.Text="<a href='show.aspx?id="+(id-1)+"'>上一篇</a>";
labTop1.Text="<a href='show.aspx?id="+(id-1)+"'>上一篇</a>";
}
private void RowCount()//计算记录总数
{

//string strConn=ConfigurationSettings.AppSettings["strCon"];
OleDbConnection Conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("../Data/AboutMeDB.mdb"));
OleDbCommand OleCmd=new OleDbCommand("select count(*) as ReSum from Document",Conn);
 OleDbDataReader OleDR=null;
try
{
Conn.Open();
OleDR=OleCmd.ExecuteReader();
if (OleDR.Read())
    Count=Convert.ToInt16(OleDR["ReSum"].ToString());
}
catch(Exception err)
{
Page.Response.Write(err.Message+"对不起,连接数据库出错!");
Page.Response.End();
}
finally
{
Conn.Close();
OleCmd.Dispose();
OleDR.Close();
}
//return Count;
}
private void GetData(int id)//显示数据
{
//string strConn=ConfigurationSettings.AppSettings["strCon"];
OleDbConnection Conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("../Data/AboutMeDB.mdb"));
OleDbDataAdapter OleCmdDoc=new OleDbDataAdapter("select * from Document where DocumentID="+id,Conn);
DataSet OleDS=new DataSet();
try
{
OleCmdDoc.Fill(OleDS,"Document");
DocumentTable=OleDS.Tables["Document"];
DocumentTable.Rows[0]["DocumentTitle"]=BoxTohtml(DocumentTable.Rows[0]["DocumentTitle"].ToString());
DocumentTable.Rows[0]["DocumentDoc"]=BoxTohtml(DocumentTable.Rows[0]["DocumentDoc"].ToString());
}
catch(Exception err)
{
Page.Response.Write(err.Message+"对不起,连接数据库出错!");
Page.Response.End();
}
} private void NewData(int id)//更新游览次数
{
//string strConn=ConfigurationSettings.AppSettings["strCon"];
OleDbConnection Conn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("../Data/AboutMeDB.mdb"));
OleDbCommand OleCmd=new OleDbCommand("update Document set DocumentNumber=DocumentNumber+1 where DocumentID="+id,Conn);
try
{
Conn.Open();
OleCmd.ExecuteNonQuery();
}
catch(Exception err)
{
Page.Response.Write(err.Message+"对不起,连接数据库出错!");
Page.Response.End();
}
finally
{
Conn.Close();
OleCmd.Dispose();
}
} private string BoxTohtml(string chr)
{
if (chr == null)
return "";
chr = chr.Trim();
chr = chr.Replace("<","&lt;");
chr = chr.Replace(">","&gt;");
chr=chr.Replace("\n","<br>");
chr=chr.Replace("\t","&nbsp;&nbsp;&nbsp;");
chr=chr.Replace(" ","&nbsp;");
return (chr);
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{    
this.butGo.Click += new System.EventHandler(this.butGo_Click);
this.butGo1.Click += new System.EventHandler(this.butGo1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void butGo1_Click(object sender, System.EventArgs e)
{
try
{
int id =Convert.ToInt16(txtID1.Text.Trim());
if (id>Count)
id=Count;
if (id<=0)
id=1;
Page.Response.Redirect("show.aspx?id="+id);
}
catch
{
labError1.Visible=true;
}
} private void butGo_Click(object sender, System.EventArgs e)
{
try
{
int id=Convert.ToInt16(txtID.Text.Trim());
if (id>Count)
id=Count;
if (id<=0)
id=1;
Page.Response.Redirect("show.aspx?id="+id);
}
catch
{
labError.Visible=true;
}
}
}
}