WebForm built-in objects, data additions and deletions, state retention

Source: Internet
Author: User

First, built-in objects

1.Response objects: responding to requests
Response.Write ("<script>alert (' Add success! ') </script> "); → Output
Response.Redirect ("default.aspx"); → Jump page

2.Request objects: getting requests
request["key"] to get the value passed in →key: the name of the definition

3.QueryString: Address bar data transfer? Key=value&key=value
Note: Things that do not need to be kept secret can be transmitted
do not pass long things, because the length is limited, too long will cause data loss

Second, the data deletion and modification

Add

Add a "Add button" link to a new window in the main interface to add a page

 <input id= btn1   " Type="  button   " Value="   add user   "/><script> document.getElementById (  " btn1  " ). onclick = function () {window.open (  tianjia.asp x  , "       _blank   ); }; </script> 

Verify that two passwords entered are consistent and inconsistent give the result to Label1 display, the button does not submit

<script type="Text/javascript">window.onload=function () {document.getElementById ("Button1"). onclick =function () {varPWD1 = document.getElementById ("TextBox2"). Value; varPWD2 = document.getElementById ("TextBox3"). Value; if(Pwd1! =pwd2) {document.getElementById ("Label1"). InnerText ="two times the password is inconsistent! "; return false;        }            };    }; </script> <style type="Text/css">#Label1 {color:red; }    </style>

Click "Add" to complete the submission

This.opener.location.href= ' default.aspx ': Refresh the main page across interfaces

    voidButton1_Click (Objectsender, EventArgs e) {        //1. Build a Users ObjectUsers U =NewUsers (); U.username=TextBox1.Text; U.password=TextBox3.Text; U.nickname=Textbox4.text; U.sex=Convert.toboolean (RadioButtonList1.SelectedItem.Value); stringDate = Dropdownlist1.selectedvalue +"-"+ Dropdownlist2.selectedvalue +"-"+Dropdownlist3.selectedvalue; U.birthday=convert.todatetime (date); U.nation=DropDownList4.SelectedItem.Value; //2. Add this object to the database        BOOLOK =NewUsersdata ().        Insert (U); //3. Prompt to add success        if(OK) {Response.Write ("<script>alert (' Add success! ') </script>"); Response.Write ("<script>this.opener.location.href= ' default.aspx '; This.close ();</script>"); }        Else{Response.Write ("<script>alert (' Add failed! ') </script>"); }    }

Delete

In the main page data display to add a column to delete, click Delete, the current window opens delete.aspx page after the deletion code to jump to the homepage, a kind of refreshing effect

Delete Column: <td><a href= "delete.aspx?un=<% #Eval (" UserName ")%>" > Delete </a></td>

To delete a page's code:

    protected voidPage_Load (Objectsender, EventArgs e) {        //1. Get the primary key value to delete, username        stringUname = request["un"].        ToString (); //2. Delete        NewUsersdata ().        Delete (Uname); //3. Recall the Display pageResponse.Redirect ("Default.aspx"); }

Modify

Main Page Data display add a column modify:<td><a href= "xiugai.aspx?un=<% #Eval (" UserName ")%>" target= "_blank" > Modify </a></td>

Modify page Data display:

    stringPWD =""; protected voidPage_Load (Objectsender, EventArgs e) {        //1, will pass over the primary key value receive        stringuname = request["un"].        ToString (); //2, the object is detected by the primary key valueUsers U =NewUsersdata ().                Select (uname); PWD= U.password;//Define a variable = original password when the user does not change the password        if(!IsPostBack) {            //re-fill the drop-down list with data             for(inti = DateTime.Now.Year; I >=1900; i--) {ListItem Li=NewListItem (i.ToString (), i.tostring ());            DropDownList1.Items.Add (LI); }             for(inti =1; I <= A; i++) {ListItem Li=NewListItem (i.ToString (), i.tostring ());            DropDownList2.Items.Add (LI); }             for(inti =1; I <= to; i++) {ListItem Li=NewListItem (i.ToString (), i.tostring ());            DropDownList3.Items.Add (LI); } Dropdownlist4.datasource=NewNationdata (). Select ();//ethnicDropdownlist4.datatextfield ="Nationname"; Dropdownlist4.datavaluefield="Nationcode";                       Dropdownlist4.databind (); //3. Bind the data in the object to each controlLabel2.Text = U.username;//account Read OnlyTextbox4.text =U.nickname; foreach(ListItem Liinchradiobuttonlist1.items) {if(u.sex) {if(Li. Value = ="True") {li. Selected=true; }                }                Else                {                    if(Li. Value = ="False") {li. Selected=true; }                }            }            //Selected Month DayDropdownlist1.selectedvalue =u.birthday.year.tostring (); Dropdownlist2.selectedvalue=u.birthday.month.tostring (); Dropdownlist3.selectedvalue=u.birthday.day.tostring (); Dropdownlist4.selectedvalue= U.nation;//Take ethnic} Button1.Click+ = button1_click;//Modify button click events    }

To modify the button code:

    voidButton1_Click (Objectsender, EventArgs e) {        //1. Build a Users ObjectUsers U =NewUsers (); U.username=Label2.Text; if(TextBox2.Text = =""&& TextBox3.Text = ="") {U.password=pwd; }        Else{U.password=TextBox3.Text; } u.nickname=Textbox4.text; U.sex=Convert.toboolean (RadioButtonList1.SelectedItem.Value); stringDate = Dropdownlist1.selectedvalue +"-"+ Dropdownlist2.selectedvalue +"-"+Dropdownlist3.selectedvalue; U.birthday=convert.todatetime (date); U.nation=DropDownList4.SelectedItem.Value; //2. Add this object to the database        BOOLOK =NewUsersdata ().        Update (U); //3. Prompt to modify success        if(OK) {Response.Write ("<script>alert (' modified successfully! ') </script>"); Response.Write ("<script>this.opener.location.href= ' default.aspx '; This.close ();</script>"); }        Else{Response.Write ("<script>alert (' Modify failed! ') </script>"); }    }

Third, the landing status is maintained

"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 the save time is not set, the session cookie is:
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

function: keep the user's login status

Usage:

1. Get account Number: response.cookies["user". Value = TextBox1.Text;

2, set the login to keep the expiration time: response.cookies["user". Expires = DateTime.Now.AddDays (7);

3. Clear cookies:response.cookies["user"]. Expires = DateTime.Now.AddDays (-5); Just leave the value negative to indicate that it has expired for several days

WebForm built-in objects, data additions and deletions, state retention

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.