- 作者:xiaoxiao
- 发表时间:2020-12-23 10:55
- 来源:未知
蛙蛙推荐:偶做的用户管理系统 因为代码比较多,只贴一下,添加用户的代码,我把表示层和业务逻辑层分开了,单独建立了一个Cls_user的类来对用户集中进行管理,里面包括了对用户的添加,删除,修改的方法,以及访问用户的名称,密码,权限等的字段,这样分开做也符合多层架构的原则,我没有单独做dal层,因为我部打算以后换数据库. 在前台的窗体中有几个下拉列表控件,我是分别用散列表和DataTable来做数据动态绑定的,也算是一个小技巧吧. 在业务逻辑的类里我用使用了方法的输出参数,大家可以顺便看一下输出参数怎么用. 先贴前台的显示代码?注意dropdownlist控件的DataTextField和DataValueField属性 add_user.aspx <%@ Page language="c#" Codebehind="adduser.aspx.cs" AutoEventWireup="false" Inherits="oa.admin.users.adduser" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>adduser</title> <meta content="False" name="vs_snapToGrid"> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> <meta content="C#" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> <LINK href="../../style/wawa.css" type="text/css" rel="stylesheet"> </HEAD> <body MS_POSITIONING="FlowLayout"> <form id="Form1" method="post" runat="server"> <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="500" border="1"> <TR> <TD align="left" colSpan="3"> <P align="center"><FONT face="宋体"></FONT><FONT face="宋体">添加用户</FONT></P> </TD> </TR> <TR> <TD style="HEIGHT: 16px"> <P align="right"><FONT face="宋体">用户名:</FONT></P> </TD> <TD style="HEIGHT: 16px" align="left"> <P align="left"><FONT face="宋体"><asp:textbox id="User_Name" runat="server"></asp:textbox></FONT></P> </TD> <TD style="HEIGHT: 16px"><FONT face="宋体"> <P align="left"><asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" ControlToValidate="User_Name" ErrorMessage="用户名不能为空"></asp:requiredfieldvalidator></P> </FONT> </TD> </TR> <TR> <TD> <P align="right"><FONT face="宋体">密码:</FONT></P> </TD> <TD align="left"> <P align="left"><FONT face="宋体"><asp:textbox id="User_Psw" runat="server" TextMode="Password"></asp:textbox></FONT></P> </TD> <TD> <P align="left"><asp:requiredfieldvalidator id="RequiredFieldValidator2" runat="server" ControlToValidate="User_Psw" ErrorMessage="密码不能为空"></asp:requiredfieldvalidator></P> </TD> </TR> <TR> <TD style="HEIGHT: 17px"> <P align="right"><FONT face="宋体">用户类型:</FONT></P> </TD> <TD style="HEIGHT: 17px" align="left"> <P align="left"><FONT face="宋体"><asp:dropdownlist id="User_Flag" runat="server" DataTextField="Key" DataValueField="Value"></asp:dropdownlist></FONT></P> </TD> <TD style="HEIGHT: 17px"> <P align="right"><FONT face="宋体"></FONT> </P> </TD> </TR> <TR> <TD> <P align="right"><FONT face="宋体">单位名称:</FONT></P> </TD> <TD align="left"> <P align="left"><FONT face="宋体"><asp:textbox id="User_DanWei" runat="server"></asp:textbox></FONT></P> </TD> <TD> <P align="left"><asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server" ControlToValidate="User_DanWei" ErrorMessage="单位名称不能为空"></asp:requiredfieldvalidator></P> </TD> </TR> <TR> <TD> <P align="right"><FONT face="宋体">地区:</FONT></P> </TD> <TD align="left"> <P align="left"><asp:dropdownlist id="User_Address" runat="server" DataTextField="s2" DataValueField="s1"></asp:dropdownlist></P> </TD> <TD> <P align="right"> </P> </TD> </TR> <TR> <TD style="HEIGHT: 17px"> <P align="right"><FONT face="宋体">用户说明:</FONT></P> </TD> <TD style="HEIGHT: 17px" align="left"><asp:textbox id="User_ShuoMing" runat="server" TextMode="MultiLine"></asp:textbox></TD> <TD style="HEIGHT: 17px"><asp:requiredfieldvalidator id="RequiredFieldValidator4" runat="server" ControlToValidate="User_ShuoMing" ErrorMessage="用户说明不能为空"></asp:requiredfieldvalidator></TD> </TR> <TR> <TD></TD> <TD align="left"><asp:button id="Button1" runat="server" Text="添加用户"></asp:button></TD> <TD><FONT face="宋体"></FONT></TD> </TR> </TABLE> </form> </body> </HTML> 再贴这个文件的后台编码类: add_user.aspx.cs using System; using System.Collections; using System.ComponentModel; using System.Data; 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 oa.admin.users { /// <summary> /// adduser 的摘要说明。 /// </summary> public class adduser : System.Web.UI.Page { protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2; protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3; protected System.Web.UI.WebControls.TextBox User_Name; protected System.Web.UI.WebControls.TextBox User_Psw; protected System.Web.UI.WebControls.DropDownList User_Flag; protected System.Web.UI.WebControls.TextBox User_DanWei; protected System.Web.UI.WebControls.DropDownList User_Address; protected System.Web.UI.WebControls.TextBox User_ShuoMing; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4; private void Page_Load(object sender, System.EventArgs e) { User_Flag.DataSource=ht_User_Flag(); User_Flag.DataBind(); ListItem li=new ListItem(); li.Value="0"; li.Text="系统管理员"; User_Flag.Items.Insert(0,li); User_Address.DataSource=dt_User_Address(); User_Address.DataBind(); // 在此处放置用户代码以初始化页面 } ICollection ht_User_Flag() { Hashtable ht=new Hashtable(); ht.Add("厅领导","1"); ht.Add("省律师协会","2"); ht.Add("省律师处","3"); ht.Add("市律师协会","4"); ht.Add("市律师处","5"); ht.Add("省直律师事务所","6"); return ht; } ICollection dt_User_Address() { //其实向这样text和value值相同的用ArrayList做绑定源也不错 DataTable dt = new DataTable(); //DataRow dr; DataRow[] dr=new DataRow[6]; dt.Columns.Add(new DataColumn("s1", typeof(string))); dt.Columns.Add(new DataColumn("s2", typeof(string))); dr[0]=dt.NewRow(); dr[0]["s1"]="石家庄"; dr[0]["s2"]="石家庄"; dt.Rows.Add(dr[0]); dr[1]=dt.NewRow(); dr[1]["s1"]="保定"; dr[1]["s2"]="保定"; dt.Rows.Add(dr[1]); dr[2]=dt.NewRow(); dr[2]["s1"]="邢台"; dr[2]["s2"]="邢台"; dt.Rows.Add(dr[2]); dr[3]=dt.NewRow(); dr[3]["s1"]="张家口"; dr[3]["s2"]="张家口"; dt.Rows.Add(dr[3]); dr[4]=dt.NewRow(); dr[4]["s1"]="衡水"; dr[4]["s2"]="衡水"; dt.Rows.Add(dr[4]); dr[5]=dt.NewRow(); dr[5]["s1"]="沧州"; dr[5]["s2"]="沧州"; dt.Rows.Add(dr[5]); DataView dv = new DataView(dt); return dv; } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) { string msg; oa.cls.Cls_user user=new oa.cls.Cls_user(); user.User_Name=User_Name.Text.ToString(); user.User_Psw=User_Psw.Text.ToString(); user.User_Flag=User_Flag.SelectedItem.Value.ToString(); user.User_DanWei=User_DanWei.Text.ToString(); user.User_Address=User_Address.SelectedItem.Value.ToString(); user.User_ShuoMing=User_ShuoMing.Text.ToString(); if(user.AddUser(out msg)) { Response.Write("<script>alert('"+msg+"');location.href='adduser.aspx';</script>"); } else{ Response.Write("没有添加成功<br>出错信息:"+msg+"<br>sql语句"+user.sql); } } } } 最后贴我写的用户管理的类的源代码: using System; using System.Data.SqlClient; using System.Configuration; namespace oa.cls { /// <summary> /// Cls_user 的摘要说明。 /// </summary> public class Cls_user { public Cls_user() { myconn=new SqlConnection((string)ConfigurationSettings.AppSettings["connstring"]);//连接数据库 myconn.Open();//打开 } public string User_Name; //用户名 public string User_Psw;//用户密码 public string User_Flag;//用户权限 public string User_DanWei;//用户单位 public string User_Address;//用户地址 public string User_ShuoMing;//用户说明 private SqlConnection myconn; public string sql; public bool AddUser(out string msg) { //用户添加 sql = "insert into [Web_User] (User_Name,User_Psw,User_Flag,User_DanWei,User_Address,User_ShuoMing) values('"+User_Name+"','"+User_Psw+"',"+User_Flag+",'"+User_DanWei+"','"+User_Address+"','"+User_ShuoMing+"')"; SqlCommand cmd = new SqlCommand(sql, myconn); msg="用户添加成功"; try { cmd.ExecuteNonQuery(); return true; } catch (SqlException exc) { msg=exc.Message; return false; } finally { myconn.Close(); } } public bool EditUser() { return true; } public bool DelUser() { return true; } } }