?? admin_user.aspx.cs
字號:
?using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Admin_Admin_User : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserID"] == "" || Session["UserID"] == null)
{
Response.Write("<script language=javascript>window.alert('為了系統安全,請您重新登陸');window.location.href=('Admin_Login.aspx')</script>");
}
if (!Page.IsPostBack)
{
this.BindToDataGrid();// 在此處放置用戶代碼以初始化頁面
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.IsValid)
{
string admin =Request["UserName"].ToString();
string pwd =Request["UserPwd"].ToString();
OleDbConnection myconn = DB.CreateDB();
myconn.Open();
OleDbCommand cmd = new OleDbCommand("insert into admin(admin,pwd) values('" + admin + "','" + pwd + "')", myconn);
cmd.ExecuteNonQuery();
Response.Write("<script language=javascript>window.alert('添加成功!');window.location.href='Admin_User.aspx';</script>");
myconn.Close();
}
}
public static bool findAdmin(string UserName)
{
OleDbConnection myconn = DB.CreateDB();
myconn.Open();
OleDbCommand cmd = new OleDbCommand("select count(*) from admin where admin='" + UserName + "'", myconn);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
return true;
}
else
{
return false;
}
}
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
string UserName = args.Value;
if (findAdmin(UserName))
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}
private void BindToDataGrid()
{
DataSet ds = new DataSet();
OleDbConnection myconn = DB.CreateDB();
myconn.Open();
OleDbCommand cmd = new OleDbCommand("select id,admin,pwd from admin", myconn);
OleDbDataAdapter sdr = new OleDbDataAdapter(cmd);
sdr.Fill(ds, "admin");
DataGrid1.DataKeyField = "id";
DataGrid1.DataSource = ds.Tables["admin"];
DataGrid1.DataBind();
myconn.Close();
}
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
string id = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
OleDbConnection myconn = DB.CreateDB();
myconn.Open();
OleDbCommand cmd = new OleDbCommand("delete from admin where id=" + id, myconn);
cmd.ExecuteNonQuery();
myconn.Close();
this.BindToDataGrid();
}
protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#1e90ff'");
e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
((LinkButton)(e.Item.Cells[4].Controls[0])).Attributes.Add("onclick", "return confirm('確認刪除?')");
}
}
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = e.Item.ItemIndex;
this.BindToDataGrid();
}
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex = -1;
this.BindToDataGrid();
}
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string id = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string UserPwd=FunStr(((TextBox)(e.Item.Cells[2].Controls[0])).Text);
//string UserPwd = ((TextBox)e.Item.FindControl("Pwd")).Text;
OleDbConnection myconn = DB.CreateDB();
myconn.Open();
OleDbCommand cmd = new OleDbCommand("Update admin Set pwd='" + UserPwd + "' where id="+id, myconn);
cmd.ExecuteNonQuery();
myconn.Close();
this.DataGrid1.EditItemIndex = -1;
this.BindToDataGrid();
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
this.BindToDataGrid();
}
public static string FunStr(string str)
{
str = str.Replace("'", "‘");
str = str.Replace(" ", " ");
str = str.Trim();
if (str.Trim().ToString() == "")
str = "無";
return str;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -