📄 a_teachers.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;
using ExpReserve;
public partial class A_Teachers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Admin aAdmin = (Admin)this.Master;
aAdmin.ManageTitle = "老师管理";
aAdmin.MenuSelectedValue = "TeacherManage";
this.fsItem.Visible = false;
Initialize();
this.btnDel.Attributes.Add("onclick", "return confirm('您真的要删除吗?');");
this.lbMsg.Text = "";
}
}
#region 属性
static int m_ColID = 0;
#endregion
#region 操作函数
/// <summary>
/// 返回所有的实验项目批次
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="sortExpression">要排序的字段</param>
private void BindDisplayList(int pageIndex, string sortExpression)
{
lbMsg.Text = "";
TeachersList.Columns[m_ColID].Visible = true;
TeachersList.SelectedIndex = -1;
DataTable dt = SingleInitials.DbAccess.GetTeachersList().Tables[0];//返回所有列表
if (sortExpression == null || sortExpression == string.Empty)
{
JKLib.Web.GridViewOperation.BindPagingGridView(TeachersList, dt.DefaultView, pageIndex);
}
else
{
JKLib.Web.GridViewOperation.BindPagingGridView(TeachersList, dt.DefaultView, pageIndex, sortExpression, true, JKLib.Web.GridViewOperation.BindPagingGridViewType.Sorting);
}
TeachersList.Columns[m_ColID].Visible = false;
}
private void Initialize()
{
this.BindDisplayList(0,"");
}
private void DisplayItem(string mID)
{
fsItem.Visible = true;
if (mID == "-1")
{
btnDel.Visible = false;
legendItem.InnerText = "新增";
NewItem();
}
else
{
btnDel.Visible = true;
legendItem.InnerText = "修改";
LoadItem(int.Parse(mID));
}
}
private bool CheckInput(ref string strError)
{
strError = "";
bool retValue = true;
if (tbxName.Text == "")
{
strError += "请输入姓名;";
retValue = false;
}
if (tbxAccount.Text.Trim() == "")
{
strError += "请输入登入账号;";
retValue = false;
}
if (tbxPassword.Text.Trim() == "")
{
strError += "请输入密码;";
retValue = false;
}
if (this.tbxKeyID.Text == "-1")//添加老师
{
if (SingleInitials.DbAccess.GetIsTeacherAccountExists(tbxAccount.Text))
{
strError += "平台账号已存在;";
retValue = false;
}
}
else//修改老师
{
ExpReserve.Teacher aTeacher = SingleInitials.DbAccess.GetATeacher(long.Parse(this.tbxKeyID.Text));
if (tbxAccount.Text != aTeacher.Account)//账号有发生改变才判断
{
if (SingleInitials.DbAccess.GetIsTeacherAccountExists(tbxAccount.Text))
{
strError += "平台账号已存在;";
retValue = false;
}
}
}
return retValue;
}
private bool Save()
{
string errStr = "";
if (!CheckInput(ref errStr))
{
lbMsg.Text = errStr;
return false;
}
try
{
if (this.tbxKeyID.Text == "-1")
{
//添加
Teacher aTeacher = new Teacher();
aTeacher.Name = this.tbxName.Text.Trim();
aTeacher.Sex = this.ddlSex.SelectedValue;
aTeacher.Department = this.tbxDepartment.Text.Trim();
aTeacher.Account = this.tbxAccount.Text.Trim();
aTeacher.Password = this.tbxPassword.Text;
return SingleInitials.DbAccess.AddATeacher(aTeacher);
}
else
{
//修改
Teacher aTeacher = new Teacher();
aTeacher.ID = long.Parse(this.tbxKeyID.Text);
aTeacher.Name = this.tbxName.Text.Trim();
aTeacher.Sex = this.ddlSex.SelectedValue;
aTeacher.Department = this.tbxDepartment.Text.Trim();
aTeacher.Account = this.tbxAccount.Text.Trim();
aTeacher.Password = this.tbxPassword.Text;
return SingleInitials.DbAccess.EditATeacher(aTeacher);
}
}
catch (Exception ex)
{
lbMsg.Text = ex.Message;
return false;
}
}
private void NewItem()
{
// 清空编辑界面
this.tbxName.Text = "";
this.ddlSex.SelectedIndex = 0;
this.tbxDepartment.Text = "";
this.tbxAccount.Text = "";
this.tbxPassword.Text = "";
if (null == tbxPassword.Attributes["value"])
{
tbxPassword.Attributes.Add("value", "");
}
else
{
tbxPassword.Attributes["value"] = "";
}
}
private void LoadItem(int mID)
{
Teacher aTeacher = SingleInitials.DbAccess.GetATeacher((long)mID);
tbxKeyID.Text = aTeacher.ID.ToString();//记录当前记录ID
tbxName.Text = aTeacher.Name;
ddlSex.SelectedValue = aTeacher.Sex;
tbxDepartment.Text = aTeacher.Department;
tbxAccount.Text = aTeacher.Account;
tbxPassword.Text = aTeacher.Password;
if (null == tbxPassword.Attributes["value"])
{
tbxPassword.Attributes.Add("value", aTeacher.Password);
}
else
{
tbxPassword.Attributes["value"] = aTeacher.Password;
}
}
#endregion
#region 表格处理
protected void BatchList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindDisplayList(e.NewPageIndex, null);
}
protected void BatchList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "TeacherEdit")
{
TeachersList.SelectedIndex = int.Parse(e.CommandArgument.ToString());
string theID = TeachersList.Rows[int.Parse(e.CommandArgument.ToString())].Cells[m_ColID].Text;
tbxKeyID.Text = theID;
DisplayItem(theID);
}
}
protected void BatchList_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Pager)
JKLib.Web.GridViewOperation.GridRowCreated(sender, e);
}
protected void BatchList_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void BatchList_Sorting(object sender, GridViewSortEventArgs e)
{
BindDisplayList(TeachersList.PageIndex, e.SortExpression);
}
#endregion
#region 列表事件处理
#endregion
#region 按钮事件
protected void btnNewBatch_Click(object sender, EventArgs e)
{
this.tbxKeyID.Text = "-1";
DisplayItem(tbxKeyID.Text);
}
protected void btnDel_Click(object sender, EventArgs e)
{
try
{
int lastPageIndex = this.TeachersList.PageIndex;
SingleInitials.DbAccess.DelATeacher(long.Parse(this.tbxKeyID.Text));
BindDisplayList(0, "");//更新列表
if (lastPageIndex < this.TeachersList.PageCount)
{
BindDisplayList(lastPageIndex, "");//更新列表
}
fsItem.Visible = false;
}
catch (Exception ex)
{
lbMsg.Text = ex.Message;
}
}
protected void btn_Save_Click(object sender, EventArgs e)
{
if (Save())
{
int lastPageIndex = this.TeachersList.PageIndex;
if ("-1" == this.tbxKeyID.Text)//添加
{
this.BindDisplayList(0, "");//更新列表
}
else//修改
{
this.BindDisplayList(lastPageIndex, "");//更新列表
}
this.fsItem.Visible = false;
}
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -