using System;
using System.IO;
using System.Text;
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.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

namespace baoge.tongxun
{
	/// <summary>
	/// View 的摘要说明。
	/// </summary>
	public class View : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Literal LiteralGuests;
		protected System.Web.UI.HtmlControls.HtmlForm View2;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//从xml文件中加载用户通讯录数据库
			XmlDocument doc = new XmlDocument( );
			doc.Load( Server.MapPath("guestbook.xml") );

			 
			string strPageAsked = Request.QueryString["page"];

			//如果给出的页面不合法，设置成1
			if ( strPageAsked == null )
			{
				strPageAsked = "1";
			}

			int nGuestPerPage = 5;
			int nGuests = doc.ChildNodes[1].ChildNodes.Count;

			int nPageAsked = System.Convert.ToInt32(strPageAsked);

			int lowerbound = 1 + ( nPageAsked - 1 ) * nGuestPerPage;
			int upperbound = lowerbound + nGuestPerPage - 1;

			//作XSLT转换
			XslTransform xslt = new XslTransform();
			xslt.Load( Server.MapPath("guestbook.xslt") );

			//建立XLST参数列表
			XsltArgumentList xsltArgs = new XsltArgumentList();
			xsltArgs.AddParam("lowerbound", "", lowerbound.ToString());
			xsltArgs.AddParam("upperbound", "", upperbound.ToString());

			//把XML转换成HTML
			MemoryStream ms = new MemoryStream();
			xslt.Transform( doc, xsltArgs, ms );
			ms.Seek( 0, SeekOrigin.Begin );

			StreamReader sr = new StreamReader(ms);

			//在View.aspx页中，插入结果
			LiteralGuests.Text = sr.ReadToEnd();

			//在页面底部插入页面导航
			int nPages = 0;
			
			if (( nGuests % nGuestPerPage) != 0 )
				nPages = 1 + (nGuests / nGuestPerPage);
			else
				nPages = (nGuests / nGuestPerPage);

			LiteralGuests.Text += "Page(s) ";

			for (int n = 1; n <= nPages; n++)
			{
				LiteralGuests.Text += "<font face='verdana' size='2'><a href='View.aspx?page=";
				LiteralGuests.Text += n.ToString();
				LiteralGuests.Text += "'>";
				LiteralGuests.Text += n.ToString();
				LiteralGuests.Text += "</a></font> ";
			}

			sr.Close();
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
