📄 admin_user.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace sms
{
public partial class userlist : System.Web.UI.Page
{
SqlConnection connections;
protected void Page_Load(object sender, System.EventArgs e)
{
if (Session["user_id"] == null)
{
Response.Redirect("default.aspx");
}
if (Session["user_id"].ToString() != "admin")
{
Response.Redirect("default.aspx");
}
string strconn = System.Configuration.ConfigurationManager.AppSettings["connstring"];
connections=new SqlConnection(strconn);
if (!IsPostBack)
{
Bindgrid();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
public void DataGrid_Page(object sender,DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
Bindgrid();
}
public void DataGrid_cancel(object sender,DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
Bindgrid();
}
public void DataGrid_edit(object sender,DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;
Bindgrid();
}
public void DataGrid_update(object sender,DataGridCommandEventArgs e)
{
string sqlstring="update users set User_password=@word where User_id=@userid";
SqlCommand commnd=new SqlCommand(sqlstring,connections);
commnd.Parameters.Add(new SqlParameter("@userid",SqlDbType.VarChar,50));
commnd.Parameters.Add(new SqlParameter("@word",SqlDbType.VarChar,50));
string colvalue=((TextBox)e.Item.Cells[1].Controls[0]).Text;
commnd.Parameters["@word"].Value=colvalue;
commnd.Parameters["@userid"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex];
commnd.Connection.Open();
try
{
commnd.ExecuteNonQuery();
lblMsg.Text="编辑成功";
DataGrid1.EditItemIndex=-1;
}
catch(SqlException)
{
lblMsg.Text="编辑失败,请检查输入!";
lblMsg.Style["color"]="red";
}
commnd.Connection.Close();
Bindgrid();
}
public void DataGrid_delete(object sender,DataGridCommandEventArgs e)
{
string sqlstring="delete from users where User_id=@userid";
SqlCommand commnd=new SqlCommand(sqlstring,connections);
commnd.Parameters.Add(new SqlParameter("@userid",SqlDbType.VarChar,50));
commnd.Parameters["@userid"].Value=DataGrid1.DataKeys[(int)e.Item.ItemIndex];
commnd.Connection.Open();
try
{
int lastPageIndex = DataGrid1.CurrentPageIndex ;
if (DataGrid1.PageCount - DataGrid1.CurrentPageIndex == 1 && DataGrid1.Items.Count == 1)
{
if (DataGrid1.PageCount > 1)
lastPageIndex = lastPageIndex - 1 ;
else
lastPageIndex = 0 ;
}
DataGrid1.CurrentPageIndex = lastPageIndex ;
commnd.ExecuteNonQuery();
lblMsg.Text="删除成功!";
}
catch(SqlException)
{
lblMsg.Text="删除失败!";
lblMsg.Style["color"]="red";
}
commnd.Connection.Close();
Bindgrid();
}
public void Bindgrid()
{ string sqlstring="select users.* from users where user_lever = 1";
SqlDataAdapter sqlda=new SqlDataAdapter(sqlstring,connections);
DataSet datas=new DataSet();
sqlda.Fill(datas);
DataGrid1.DataSource=datas;
DataGrid1.DataBind();
}
protected void Datagrid_userinformation_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
protected void btnOK_Click(object sender, System.EventArgs e)
{
if(txtName.Text=="" || txtPassword.Text==""||txtRepwd.Text=="")
{
Response.Write("<script language=javascript>alert('请输入完整信息!');</script>");
return;
}
if (txtPassword.Text != txtRepwd.Text)
{
Response.Write("<script language=javascript>alert('两次输入的密码不相同!');</script>");
return;
}
SqlCommand commnd=new SqlCommand("addmanager",connections);
commnd.CommandType=CommandType.StoredProcedure;
try
{
commnd.Parameters.Add(new SqlParameter("@user_id",SqlDbType.VarChar,50));
commnd.Parameters["@user_id"].Value=txtName.Text;
commnd.Parameters.Add(new SqlParameter("@user_password",SqlDbType.VarChar,50));
commnd.Parameters["@user_password"].Value=txtPassword.Text;
commnd.Parameters.Add(new SqlParameter("@user_lever",SqlDbType.Int));
commnd.Parameters["@user_lever"].Value = 1;
commnd.Connection.Open();
commnd.ExecuteNonQuery();
lblMsg.Text="<b>添加成功!</b>";
}
catch
{
lblMsg.Text="<b>添加失败!</b>";
lblMsg.Style["color"]="red";
}
Bindgrid();
commnd.Connection.Close();
}
protected void lbtnBack_Click(object sender, EventArgs e)
{
Response.Redirect("admin_stu.aspx");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -