WebForm built-in objects and webform built-in objects

Source: Internet
Author: User

WebForm built-in objects and webform built-in objects

Built-in object:

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, modify, and query

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>
 

 

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 ();
Try
{
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 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> 
 

 

 

Default gender, three dropdownlists

<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 }}

Cookies

Create a login interface

<Form id = "form1" runat = "server">
Username: <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 the logon status for 7 days </label> <br/>

<Asp: Button ID = "Button1" runat = "server" Text = "login"/>
</Form>

Click Event

 protected void Page_Load(object sender, EventArgs e)    {        Button1.Click += Button1_Click;    }    void Button1_Click(object sender, EventArgs e)    {        bool ok = new UsersData().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 Persistence:
Cookies

A piece of text saved on the hard drive of your computer

The http protocol includes a browser that allows websites to temporarily store data on users' computers in the form of Cookies.

If no storage time is set, session cookies
1. If you do not refresh the page again within 20 minutes, the cookies will be automatically deleted.
2. When the current access connection is interrupted, such as closing the browser, the cookies will be automatically deleted.

Purpose:
Maintain the user's login status

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.