📄 employeelist.aspx.cs
字号:
using System;
using System.Data;
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 employeeList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
emListDBConn dbConn = new emListDBConn();
ArrayList users = dbConn.getAllUser();
TableRow row = null;
TableCell cell = null;
ImageButton imgEdit = null;
ImageButton imgDel = null;
TextBox txtPw = null;
int rowNum = 0;
foreach (Employee user in users)
{
row = new TableRow();
rowNum++;
//用户名
cell = new TableCell();
cell.Text = user.ID;
row.Cells.Add(cell);
//密码
cell = new TableCell();
txtPw = new TextBox();
txtPw.Text = user.ID;
txtPw.ReadOnly = true;
cell.Controls.Add(txtPw);
row.Cells.Add(cell);
//权限
cell = new TableCell();
DropDownList list = new DropDownList();
list.Items.Add(new ListItem("前台"));
list.Items.Add(new ListItem("经理"));
if (user.Authority == 1)
{
list.SelectedValue = "经理";
}
else
{
list.SelectedValue = "前台";
}
list.Enabled = false;
cell.Controls.Add(list);
row.Cells.Add(cell);
//编辑
cell = new TableCell();
imgEdit = new ImageButton();
imgEdit.ID = rowNum.ToString();
imgEdit.ImageUrl = "pic/edit.gif";
imgEdit.Click += new ImageClickEventHandler(imgEdit_Click);
cell.Controls.Add(imgEdit);
row.Cells.Add(cell);
//删除
cell = new TableCell();
imgDel = new ImageButton();
imgDel.ID = "del_" + user.ID;
imgDel.ImageUrl = "pic/del.gif";
imgDel.Click += new ImageClickEventHandler(imgDel_Click);
cell.Controls.Add(imgDel);
row.Cells.Add(cell);
Table2.Rows.Add(row);
}
}
void imgDel_Click(object sender, ImageClickEventArgs e)
{
emListDBConn dbConn = new emListDBConn();
string userId = ((ImageButton)sender).ID.Substring(4);
dbConn.delUser(userId);
Response.Redirect("employeeList.aspx");
}
void imgEdit_Click(object sender, ImageClickEventArgs e)
{
int rowNum = Convert.ToInt32(((ImageButton)sender).ID);
if (((ImageButton)sender).ImageUrl == "pic/edit.gif")
{
//密码
foreach (TextBox txt in Table2.Rows[rowNum].Cells[1].Controls)
{
txt.ReadOnly = false;
}
//权限
foreach (DropDownList list in Table2.Rows[rowNum].Cells[2].Controls)
{
list.Enabled = true;
}
//保存
((ImageButton)sender).ImageUrl = "pic/save.bmp";
}
else
{
string id = "";
string pw = "";
int au = -1;
id = Table2.Rows[rowNum].Cells[0].Text;
foreach (TextBox txt in Table2.Rows[rowNum].Cells[1].Controls)
{
txt.ReadOnly = true;
pw = txt.Text;
}
foreach (DropDownList list in Table2.Rows[rowNum].Cells[2].Controls)
{
list.Enabled = false;
if (list.SelectedValue == "前台")
{
au = 0;
}
else
{
au = 1;
}
}
((ImageButton)sender).ImageUrl = "pic/edit.gif";
//保存记录
emListDBConn dbConn = new emListDBConn();
dbConn.updateUser(id, pw, au);
}
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string id = null;
string pw = null;
int au = -1;
foreach (Control txt in Table1.Rows[1].Cells[0].Controls)
{
if (txt is TextBox)
{
if (((TextBox)txt).Text != "")
{
id = ((TextBox)txt).Text;
}
}
}
foreach (Control txt in Table1.Rows[1].Cells[1].Controls)
{
if (txt is TextBox)
{
if (((TextBox)txt).Text != "")
{
pw = ((TextBox)txt).Text;
}
}
}
foreach (Control list in Table1.Rows[1].Cells[2].Controls)
{
if (list is DropDownList)
{
if (((DropDownList)list).SelectedValue == "前台")
{
au = 0;
}
else if (((DropDownList)list).SelectedValue == "经理")
{
au = 1;
}
}
}
if (id != null && pw != null)
{
try
{
emListDBConn dbConn = new emListDBConn();
dbConn.insertUser(id, pw, au);
Response.Redirect("employeeList.aspx");
}
catch (System.Exception err)
{
if (err is System.Data.SqlClient.SqlException)
{
Response.Write("<script>window.alert('用户名已被使用');</script>");
}
}
}
else
{
Response.Write("<script>window.alert('用户名/密码/权限为空');</script>");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -