Frog frog recommendation: even a user management system
Because there are many codes, just paste them and add the user's code. I separated the presentation layer from the business logic layer and created a cls_user class to manage users in a centralized manner, it includes the methods for adding, deleting, and modifying a user, as well as fields for accessing the user's name, password, and permissions. This separation also conforms to the principle of multi-layer architecture, I didn't do the Dal layer separately, because my department intends to change the database later.
There are several drop-down list controls in the front-end form. I use the hash list and datatable to dynamically bind data, which is also a small trick.
In the business logic class, I used the output parameters of the method. You can take a look at how to use the output parameters.
First paste the front-end display code? Note the datatextfield and datavaluefield attributes of the dropdownlist control.
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 = ""> Add a user </font> </P>
</TD>
</Tr>
<Tr>
<TD style = "height: 16px">
<P align = "right"> <font face = ""> User name: </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 = "username cannot be blank"> </ASP: requiredfieldvalidator> </P>
</Font>
</TD>
</Tr>
<Tr>
<TD>
<P align = "right"> <font face = ""> password: </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 = "password cannot be blank"> </ASP: requiredfieldvalidator> </P>
</TD>
</Tr>
<Tr>
<TD style = "height: 17px">
<P align = "right"> <font face = ""> User Type: </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 = ""> Organization Name: </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 = "the unit name cannot be blank"> </ASP: requiredfieldvalidator> </P>
</TD>
</Tr>
<Tr>
<TD>
<P align = "right"> <font face = ""> region: </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 = ""> User Description: </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 = "User description cannot be blank"> </ASP: requiredfieldvalidator> </TD>
</Tr>
<Tr>
<TD> </TD>
<TD align = "Left"> <asp: button id = "button1" runat = "server" text = "Add User"> </ASP: button> </TD>
<TD> <font face = ""> </font> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
Paste the background encoding class of this file:
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>
/// Summary of 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 = "System Administrator ";
User_flag.items.insert (0, Li );
User_address.datasource = dt_user_address ();
User_address.databind ();
// Place user code here to initialize the page
}
Icollection ht_user_flag ()
{
Hashtable ht = new hashtable ();
Ht. Add ("department leader", "1 ");
Ht. Add ("provincial bar association", "2 ");
Ht. Add ("provincial Attorney Department", "3 ");
Ht. Add ("Municipal Bar Association", "4 ");
Ht. Add ("city attorney office", "5 ");
Ht. Add ("provincial law firm", "6 ");
Return HT;
}
Icollection dt_user_address ()
{
// In fact, it is good to use arraylist to bind the source with the same text and value.
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"] = "Shijiazhuang ";
Dr [0] ["S2"] = "Shijiazhuang ";
DT. Rows. Add (Dr [0]);
Dr [1] = DT. newrow ();
Dr [1] ["S1"] = "Baoding ";
Dr [1] ["S2"] = "Baoding ";
DT. Rows. Add (Dr [1]);
Dr [2] = DT. newrow ();
Dr [2] ["S1"] = "Xingtai ";
Dr [2] ["S2"] = "Xingtai ";
DT. Rows. Add (Dr [2]);
Dr [3] = DT. newrow ();
Dr [3] ["S1"] = "Zhangjiakou ";
Dr [3] ["S2"] = "Zhangjiakou ";
DT. Rows. Add (Dr [3]);
Dr [4] = DT. newrow ();
Dr [4] ["S1"] = "Hengshui ";
Dr [4] ["S2"] = "Hengshui ";
DT. Rows. Add (Dr [4]);
Dr [5] = DT. newrow ();
Dr [5] ["S1"] = "Cangzhou ";
Dr [5] ["S2"] = "Cangzhou ";
DT. Rows. Add (Dr [5]);
Dataview DV = new dataview (DT );
Return DV;
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
Private void initializecomponent ()
{
This. button1.click + = new system. eventhandler (this. button#click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void button#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_ps1_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 ("not added successfully <br> error message:" + MSG + "<br> SQL statement" + User. SQL );
}
}
}
}
Finally, paste the source code of the User-Managed class I wrote:
Using system;
Using system. Data. sqlclient;
Using system. configuration;
Namespace Oa. CLs
{
/// <Summary>
/// Summary of cls_user.
/// </Summary>
Public class cls_user
{
Public cls_user ()
{
Myconn = new sqlconnection (string) configurationsettings. etettings ["connstring"]); // connect to the database
Myconn. open (); // open
}
Public String user_name; // User Name
Public String user_psw; // User Password
Public String user_flag; // user permission
Public String user_danwei; // user unit
Public String user_address; // user address
Public String user_shuoming; // user description
Private sqlconnection myconn;
Public String SQL;
Public bool adduser (out string MSG)
{
// Add a user
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 = "User Added successfully ";
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;
}
}
}