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.IO;
using System.Web.Caching;

namespace DataCache
{
	/// <summary>
	/// WebForm1 的摘要说明。
	/// </summary>
	public class WebForm1 : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.TextBox CustomEamil;
		protected System.Web.UI.WebControls.TextBox CustomPass;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.Button LoginMsg;
		protected System.Web.UI.WebControls.Button Refresh;
		protected System.Web.UI.WebControls.Label Label4;
		protected System.Web.UI.WebControls.DataGrid MyDataGrid;
		protected System.Web.UI.WebControls.Label ErrorMsg;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!IsPostBack) 
			{
				LoadData();
			}
		}

		#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.LoginMsg.Click += new System.EventHandler(this.LoginMsg_Click);
			this.Refresh.Click += new System.EventHandler(this.Refresh_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void LoginMsg_Click(object sender, System.EventArgs e)
		{
			if(!Page.IsValid) 
			{
				ErrorMsg.Text = "必需输入登录信息";
				return;
			}
			DataSet ds = new DataSet();
			FileStream fs = new FileStream(Server.MapPath("LoginMsg.xml"),FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
			StreamReader reader = new StreamReader(fs);
			ds.ReadXml(reader);
			fs.Close();
			DataTable dt = ds.Tables[0];
			DataRow newrow = dt.NewRow();
			newrow["custom_email"] = CustomEamil.Text;
			newrow["custom_pass"] = CustomPass.Text;
			newrow["login_time"] = DateTime.Now.ToString();
			fs = new FileStream(Server.MapPath("LoginMsg.xml"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
			TextWriter writer = new StreamWriter(fs);
			writer = TextWriter.Synchronized(writer);
			ds.Tables[0].Rows.Add(newrow);
			ds.WriteXml(writer);
			writer.Close();
			Cache.Remove("MyData");
			LoadData();
		}

		private void Refresh_Click(object sender, System.EventArgs e)
		{
			LoadData();
		}

		public void LoadData() 
		{
			DataView Source = (DataView)Cache["MyData"];
			if(Source == null) 
			{
				DataSet ds = new DataSet();
				FileStream fs = new FileStream(Server.MapPath("LoginMsg.xml"), FileMode.Open,FileAccess.Read);
				StreamReader reader = new StreamReader(fs);
				ds.ReadXml(reader);
				fs.Close();
				Source = new DataView(ds.Tables[0]);
				Cache.Insert("MyData", Source, new CacheDependency(Server.MapPath("LoginMsg.xml")));
				ErrorMsg.Text = "从文件中读取的数据";
			}
			else 
			{
				ErrorMsg.Text = "从缓存中读取的数据";
			}
			MyDataGrid.DataSource = Source;
			MyDataGrid.DataBind();
		}
	}
}
