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;

namespace baoge.Admin
{
	/// <summary>
	/// Admin_EditUser 的摘要说明。
	/// </summary>
	public class Admin_EditUser : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.TextBox AdminName;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
		protected System.Web.UI.WebControls.TextBox UPassword;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
		protected System.Web.UI.WebControls.TextBox QQ;
		protected System.Web.UI.WebControls.TextBox MSN;
		protected System.Web.UI.WebControls.TextBox Tel;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;
		protected System.Web.UI.WebControls.TextBox Address;
		protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator5;
		protected System.Web.UI.WebControls.Label Purview;
		protected System.Web.UI.WebControls.Label UserName;
		protected System.Web.UI.WebControls.Label Sex;
		protected System.Web.UI.WebControls.Button Submit;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面


			if(Session["AdminName"]==null)
			{
				Response.Redirect("Admin_Login.aspx");
			}
			if(!IsPostBack)
			{
				EditUserDataBind();
			}
			
		}


		private void EditUserDataBind()
		{
			//取得数据库路径
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义连接字符串
			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
			//创建数据库对像
			OleDbConnection ACconn=new OleDbConnection(ACconnStr);
			//数据库操作
			try
			{
				ACconn.Open();
				//取得所需操作的记录ID
				int ID=Convert.ToInt32(Request.QueryString["ID"].Trim());

				//定义SQL语句
				string ACsql="select ID,AdminName,Password,UserName,Purview,QQ,MSN,SEX,Tel,Address from Admin where ID="+ID;
				//创建Command对像
				OleDbCommand ACcomm=new OleDbCommand(ACsql,ACconn);
				//通过执行Command实例化DataReader类
				OleDbDataReader ACdr=ACcomm.ExecuteReader();
				if(ACdr.Read())
				{
					AdminName.Text=ACdr["AdminName"].ToString();
					UPassword.Text=ACdr.GetString(2);
					if(ACdr["purview"].ToString()=="1")
					{
					  Purview.Text="系统管理员";

					}
					else if(ACdr["purview"].ToString()=="2")
					{
						Purview.Text="普通管理员";
					}
					else if(ACdr["purview"].ToString()=="3")
					{
						Purview.Text="文字编辑员";
					}
					else
					{
						Purview.Text="添加时没有选择级别";
					}
					QQ.Text=ACdr["QQ"].ToString();
					MSN.Text=ACdr["MSN"].ToString();
					UserName.Text=ACdr["UserName"].ToString();
					Sex.Text=ACdr["SEX"].ToString();
					Tel.Text=ACdr["Tel"].ToString();
					Address.Text=ACdr["Address"].ToString();
				}
			
			}
			catch(Exception ex)
			{
				Response.Write(ex.ToString());
			}
			finally
			{
				ACconn.Close();

			}
		}


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

		}
		#endregion

		private void Submit_Click(object sender, System.EventArgs e)
		{
		

			int ID=Convert.ToInt32(Request.QueryString["ID"].Trim());
			string Pv;
			//取得数据库路径
			string dbPath=System.Configuration.ConfigurationSettings.AppSettings["dbPath"];
			//定义连接字符串
			string ACconnStr="Provider=Microsoft.Jet.OleDb.4.0;Data Source="+Server.MapPath(dbPath);
			//创建数据库对像
			OleDbConnection ACconn=new OleDbConnection(ACconnStr);
			//数据库操作
			try
			{
				ACconn.Open();

				if(Purview.Text=="系统管理员")
				{
					Pv="1";

				}
				else if(Purview.Text=="普通管理员")
				{
					Pv="2";
				}
				else if(Purview.Text=="文字编辑员")
				{
					Pv="3";
				}
				else
				{
					Pv="0";
				}

				//定义SQL语句
				string ACsql1="update Admin set AdminName='"+AdminName.Text+"',[Password]='"+UPassword.Text+"',Purview='"+Pv+"',QQ='"+QQ.Text+"',MSN='"+MSN.Text+"',UserName='"+UserName.Text+"',SEX='"+Sex.Text+"',Tel='"+Tel.Text+"',Address='"+Address.Text+"' where ID="+ID;
				//创建Command对象
				OleDbCommand ACcomm=new OleDbCommand(ACsql1,ACconn);
				//执行SQL
				ACcomm.ExecuteNonQuery();
				Response.Write("<script>alert('用户资料修改成功');</script>");
				Response.Write("<script>history.go(-2);</script>");


			}
			catch(Exception ex)
			{
				Response.Write(ex.ToString());
			}
			finally
			{
				ACconn.Close();
			}

		}
	}
}
