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 baoge.Admin;
using System.Data.OleDb;
namespace baoge.Admin
{
	/// <summary>
	/// lockip 的摘要说明。
	/// </summary>
	public class lockip : Admin.Basic_class
	{
		protected System.Web.UI.WebControls.TextBox Textbox1;
		protected System.Web.UI.WebControls.TextBox ipaddress;
		protected System.Web.UI.WebControls.Button add_ip;
		protected System.Web.UI.WebControls.CustomValidator Cv_id;
		protected System.Web.UI.WebControls.DataGrid allip;
		protected System.Web.UI.WebControls.RequiredFieldValidator checkname;
		protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
		protected System.Web.UI.WebControls.ValidationSummary show_error;
		protected string sql;
		protected int startIndex=0;
		protected other ubbid=new other();
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(ubbid.check_ip(ubbid.getip()))
			{
				Response.Write("<script>alert(\"对不起,你的IP已被锁定\");window.location.href='../Error/404b.htm';</script>");
			}
			else
			{
				if(Session["AdminName"]==null)
				{
					Response.Redirect("Admin_Login.aspx");
				}
			}
			if(!IsPostBack)
			{
				allipbind();
			}
		}


		private void allipbind()
		{
			ds=new DataSet();
			sql="select id,ip from lockip order by id";
			Cmd=new OleDbDataAdapter(sql,Con);
			Cmd.Fill(ds,startIndex,allip.PageSize,"curdata");
			Cmd.Fill(ds,"tb1");
			allip.VirtualItemCount=ds.Tables["tb1"].Rows.Count;
			allip.DataSource=ds.Tables["curdata"];
			bd:
				try
				{
					allip.DataBind();
				}
				catch
				{
					allip.CurrentPageIndex--;
					goto bd;
				}
			ds.Dispose();
		}
		public void change_page(object sender, DataGridPageChangedEventArgs e)
		{
			allip.CurrentPageIndex = e.NewPageIndex;
			startIndex=allip.PageSize*allip.CurrentPageIndex;
			allipbind();
		}
		public void allip_delete(object sender,DataGridCommandEventArgs e)
		{
			sql="delete from lockip where id="+(int)allip.DataKeys[e.Item.ItemIndex];
			cmd=new OleDbCommand(sql,Con);
			Con.Open();
			cmd.ExecuteNonQuery();
			Con.Close();
			allipbind();
		}

		private void Cv_id_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
		{
			sql="select id from lockip where ip='"+ipaddress.Text.Trim()+"'";
			cmd=new OleDbCommand(sql,Con);
			Con.Open();
			dr=cmd.ExecuteReader();
			if(dr.Read())
			{
				args.IsValid=false;
			}
			else
			{
				args.IsValid=true;
			}
			Con.Close();
		}

		private void add_ip_Click(object sender, System.EventArgs e)
		{
			if(Page.IsValid)
			{
				sql="insert into lockip(ip) values('"+ipaddress.Text.Trim()+"')";
				cmd=new OleDbCommand(sql,Con);
				Con.Open();
				cmd.ExecuteNonQuery();
				Con.Close();
				allipbind();
			}
		}
		public void pageset(object sender,DataGridItemEventArgs e)
		{
            
			Admin.static_class.Set_Page(e,allip);
		}
		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			
			this.add_ip.Click += new System.EventHandler(this.add_ip_Click);
			this.Cv_id.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.Cv_id_ServerValidate);
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion
	}
}
