Webform built-in objects (Response object, Request object, QueryString), webformquerystring
Response object: Response Request
Response. Write ("<script> alert ('added successfully! ') </Script> ");
Response. Redirect ("Default. aspx ");
Request object: GET Request
Request ["key"] to obtain the passed Value
QueryString: What is data transfer in the address bar? Key = value & key = value
Note: confidential information is not required.
Do not upload long data because the length is limited, which may cause data loss.
Add, delete, and modify a data table:
Add User in Default. aspx
<Input id = "btn1" type = "button" value = "Add User"/> <script> document. getElementById ("btn1 "). onclick = function () {window. open ("defa3.3.aspx", "_ self") ;}; </script>
First, create an add method for the data category class.
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 (); try {cmd. executeNonQuery (); isok = true;} catch {} conn. close (); return isok ;}
Add:
<Body> <form id = "form1" runat = "server">
Password JS Verification
<Script type = "text/javascript"> window. onload = function () {/* document operation to retrieve the content in 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 different password inputs"; return false;/* different passwords prevent refresh and refresh */}};}; </script> <style type = "text/css"> # Label2 {color: red; /* red text displayed in Label2 */} </style>
Gender is selected by default. Three dropdownlists are required for birthdate.
<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 <= 12; I ++) {// month ListItem li = new ListItem (I. toString (), I. toString (); DropDownList3.Items. add (li) ;}for (int I = 1; I <= 31; I ++) {// day ListItem li = new ListItem (I. toString (), I. toString (); DropDownList4.Items. add (li) ;}// retrieve the national data DropDownList1.DataSource = new NationDA (). select (); DropDownList1.DataTextField = "NationName"; DropDownList1.DataValueField = "NationCode"; DropDownList1.DataBind ();} Button1.Click + = button#click; // event Delegate}
Void button#click (object sender, EventArgs e)
{
// 1. Construct 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. A message is displayed, indicating that the instance is successfully added.
If (OK)
{
Response. Write ("<script> alert ('added successfully! ') </Script> ");
Response. Redirect ("Default. aspx ");
}
Else
{
Response. Write ("<script> alert ('failed to add! ') </Script> ");
}
// 4. Close this page and refresh the display page
}
}
Ii. Delete
Operation: Add a column to the data display on the default. aspx homepage, click delete, open a new webpage, delete. aspx, run the code, and close the column. Refresh the homepage.
<Td> operation </td> <a href = "delete. aspx "? Un = <% # Eval ("UserName ")
> Delete </a> </td>
// Delete Code executed on the new webpage // 1. Obtain the primary key value and username to be deleted. The deletion method is string Uname = Request ["un"]. toString (); GET request // 2. Delete new UsersDA (). delete (Uname); // 3. Call back the Main page Response. redirect ("Main. aspx ");
3. Modify
Add a column to the new form xiugai. aspx data display page and click to enter xiugai. aspx
<Td> <a href = "#"> modify </a> </td>
How to add a data operation class:
public bool Update(Users u) { bool isok = false; cmd.CommandText = "update Users set PassWord=@b,NickName=@c,Sex=@d,Birthday=@e,Nation=@f where UserName=@a"; 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; }
// Step 1: Construct a Users object Users u = new Users (); u. userName = Label1.Text; if (TextBox3.Text = "" & TextBox4.Text = "") {// u used to determine the password. 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 = C Onvert. toDateTime (data); u. nation = DropDownList1.SelectedItem. value; // Step 2: add the object to the database. First, modify bool OK = new UsersDA () in UserDA (). update (u); // Step 3. Prompt if (OK) {Response. write ("<script> alert ('modification successful! ') </Script> "); Step 4: Response. write ("<script> this. opener. location. href = 'main. aspx '; this. close (); </script> "); // Response. redirect ("Main. aspx "); // reset the item} else {Response. write ("<script> alert ('modification failed! ') </Script> ");} // 4. Close this page and refresh the display page. // write with JS }}