Built-in objects:
response object: Response request
Response.Redirect ("default.aspx");
request object: Get request
querystring: Address bar data transfer? key= Value&key=value
Note: Things that don't need to be classified can be passed
Delete and change
adding users in Default.aspx
<input id= "btn1" type= "button" value= "Add User"/> <script> document.getElementById ("Btn1"). onclick = function () { window.open ("default3.aspx", "_self"); }; </script>
Add (method)
Public bool Insert (Users u)
{//Add
bool isOK = false;
Cmd.commandtext = "INSERT into Users values (@a,@b,@c,@d,@e,@f)";
cmd. Parameters.clear ();
cmd. Parameters.Add ("@a", u.username);
cmd. Parameters.Add ("@b", U.password);
cmd. Parameters.Add ("@c", u.nickname);
cmd. Parameters.Add ("@d", u.sex);
cmd. Parameters.Add ("@e", u.birthday);
cmd. Parameters.Add ("@f", u.nation);
Conn. Open ();
{
cmd. ExecuteNonQuery ();
isOK = true;
catch {}
Conn. Close ();
return isok;
<body> <form id= "Form1" runat= "Server" >
Password JS Verification
<script type= "Text/javascript" > window.onload = function () {/*document operation take out the contents of the Password box * / document.getElementById ("Button1"). onclick = function () { var pwd1 = document.getElementById ("TextBox3"). Value; var pwd2 = document.getElementById ("TextBox4"). Value; /* Alert (PWD1), check * */ * alert (PWD2); */ if (pwd1! = pwd2) { document.getElementById ("Label2"). InnerText = "Two times password input inconsistency"; Return false;/* password to prevent the refresh, just like refreshing * *} ; }; </script> <style type= "Text/css" > #Label2 { color:red;/*label2 text displayed in red */ } </style>
Default sex, do three DropDownList
<asp:listitem value= "true" selected= "true" > Male </asp:ListItem>
protected void Page_Load (object sender, EventArgs e) {if (! IsPostBack)//Data binding {for (int i = DateTime.Now.Year; I >= 1900; i--) {//Add year ListItem li = new ListItem (i.ToString (), i.tostring ()); DropDownList2.Items.Add (LI); } for (int i = 1; i <=; i++) {//month ListItem li = new ListItem (i.) ToString (), i.tostring ()); DropDownList3.Items.Add (LI); } for (int i = 1; i <=; i++) {//day ListItem li = new ListItem (i.) ToString (), i.tostring ()); DropDownList4.Items.Add (LI); }//Take out the national data Dropdownlist1.datasource = new Nationda (). Select (); Dropdownlist1.datatextfield = "Nationname"; Dropdownlist1.datavaluefield = "Nationcode"; Dropdownlist1.databind (); } button1.click + =button1_click;//Event Delegate}
void Button1_Click (object sender, EventArgs e)
{
///1, build a Users Object
Users U = new users ();
u.username = TextBox1.Text;
U.password = TextBox3.Text;
u.nickname = Textbox4.text;
u.sex = Convert.toboolean (RadioButtonList1.SelectedItem.Value);
string date = Dropdownlist1.selectedvalue + "-" + Dropdownlist2.selectedvalue + "-" + dropdownlist3.selectedvalue;
U.birthday = convert.todatetime (date);
u.nation = DropDownList4.SelectedItem.Value;
2. Add this object to the database
bool OK = new Usersdata (). Insert (U);
3. Prompt to add success
if (OK)
{
Response.Write ("<script>alert (' Add success! ') </script> ");
Response.Redirect ("default.aspx");
}
Else
{
Response.Write ("<script>alert (' Add failed! ') </script> ");
}
4. Close this page to refresh the display page
}
}
Second, delete
Action, add a column to the Default.aspx home data display, click Delete, open a new page delete.aspx execute the code, close, refresh the main page
<td> operations </td> <td><a href= "delete.aspx"? un=<% #Eval ("UserName")
> Delete </a></td>
Delete code executed in new page//1, get primary key value to delete, username, do Delete method string Uname = request["un"]. ToString (); GET request //2, delete new Usersda (). Delete (Uname); 3, recall the Main page response.redirect ("main.aspx");
Third, modify
New Form Xiugai.aspx Data presentation page Add a list of changes, click Enter xiugai.aspx
<td><a href= "#" > Modify </a></td>
To add a method to the data manipulation class:
public bool Update (Users u) { bool isOK = false; Cmd. CommandText = "Update Users set [email protected],[email protected],[email protected],[email protected],[email protected ] where [email protected] "; Cmd. Parameters.clear (); Cmd. Parameters.Add ("@a", u.username); Cmd. Parameters.Add ("@b", U.password); Cmd. Parameters.Add ("@c", u.nickname); Cmd. Parameters.Add ("@d", u.sex); Cmd. Parameters.Add ("@e", u.birthday); Cmd. Parameters.Add ("@f", u.nation); Conn. Open (); Try { cmd. ExecuteNonQuery (); isOK = true; } Catch {} conn. Close (); return isok; }
1 steps, building a Users object users u = new users (); U.username = Label1.Text; if (TextBox3.Text = = "" && Textbox4.text = = ") {//The u.password of the password = pwd; } else {u.password = TextBox3.Text; } u.nickname = Textbox6.text; U.sex = Convert.toboolean (RadioButtonList1.SelectedItem.Value); String data = Dropdownlist2.selectedvalue + "-" + Dropdownlist3.selectedvalue + "-" + dropdownlist4.selectedvalue; U.birthday = convert.todatetime (data); U.nation = DropDownList1.SelectedItem.Value; 2 steps, add this object to the database, first modify the method in Userda bool OK = new USERSDA (). Update (U); 3 steps, prompt to modify success if (OK) {Response.Write ("<script>alert" (' Modified successfully! ') </script> "); 4 Steps, Response.Write ("<script>this.opener.location.href= ' main.aspx '; This.close ();</script>"); Response.Redirect ("main.aspx");//re-set} else {Response. Write ("<script>alert (' Modify failed! ') </script> "); }//4, close this page, refresh the display page//with JS write}}
Cookies
Make a login interface
<form id= "Form1" runat= "Server" >
User name: <asp:textbox id= "TextBox1" runat= "Server" ></asp:textbox><br/>
Password: <asp:textbox id= "TextBox2" runat= "Server" ></asp:textbox><br/>
<asp:checkbox id= "CheckBox1" runat= "Server"/><label for= "CheckBox1" > Remember login status 7 days </label><br/>
<asp:button id= "Button1" runat= "server" text= "login"/>
</form>
Click events
protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; } voidButton1_Click (Objectsender, EventArgs e) { BOOLOK =NewUsersdata (). Select (TextBox1.Text, TextBox2.Text); if(OK) {response.cookies["User"]. Value =TextBox1.Text; if(checkbox1.checked) {response.cookies["User"]. Expires = DateTime.Now.AddDays (7); } Response.Redirect ("Default.aspx"); } }
Login status remains:
Cookies
A piece of text saved on the hard drive of the user's computer
The HTTP protocol includes a browser that allows the site to temporarily store data in the form of cookies on the user's computer.
If no save time is set, session cookies
1, if you do not refresh the page in 20 minutes, then this cookie will be automatically deleted
2, when the current access connection is interrupted, such as closing the browser, then the cookies will be automatically deleted
Role:
Keep the user's login status
WebForm Built-in objects