⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 admin_user.aspx.cs

📁 企业网站管理系统(C#.NET2003开发),供初学者学习!
💻 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("   ", "&nbsp;");

        str = str.Trim();
        if (str.Trim().ToString() == "")
            str = "无";
        return str;
    } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -