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 SK.MemberBLL;

namespace SK.MemberWeb.admin
{
	/// <summary>
	/// Provinces 的摘要说明。
	/// </summary>
	public class Provinces : BasePage
	{
		protected System.Web.UI.WebControls.DropDownList dpCountry;
		protected System.Web.UI.WebControls.Repeater lstProvince;
		CountrySystem country = new CountrySystem();
		protected System.Web.UI.WebControls.TextBox txtProvinceName;
		protected System.Web.UI.WebControls.Button btnAdd;
		ProvinceSystem province = new ProvinceSystem();
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// 在此处放置用户代码以初始化页面
			txtProvinceName.Attributes["onkeypress"] = this.GetExecuteControlScript(btnAdd);
			btnAdd.Attributes["onclick"] = "return CheckInput();";
			this.GiveFocus(txtProvinceName);
			if(!IsPostBack)
			{
				ViewState["CountryID"] = Request.QueryString["id"];
				BindDrop();
				BindData();
			}
		}

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

		}
		#endregion
		protected void Delete_Load(object sender, System.EventArgs e)
		{
			((LinkButton)sender).Attributes["onclick"]="return confirm('您确定要删除这个省份吗？');";
		}
		private void BindDrop()
		{
			dpCountry.DataSource = country.List();
			dpCountry.DataTextField = "CountryName";
			dpCountry.DataValueField = "CountryID";
			dpCountry.DataBind();
			dpCountry.SelectedValue = ViewState["CountryID"].ToString();
		}
		private void BindData()
		{
			lstProvince.DataSource = province.List(dpCountry.SelectedValue);
			lstProvince.DataBind();
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			province.Add(dpCountry.SelectedValue,txtProvinceName.Text);
			Response.Redirect("Provinces.aspx?id="+ViewState["CountryID"].ToString());
		}

		private void lstProvince_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
		{
			if(e.CommandName=="Delete")
			{
				province.Delete(e.CommandArgument);
			}
			Response.Redirect("Provinces.aspx?id="+ViewState["CountryID"].ToString());
		}
	}
}
