Cookies in the manner in which the state is stored in the program

Source: Internet
Author: User

The way in which the state is stored in the program is a cookie that was previously written about ViewState. Now continue to summarize the way cookies

New test Page Login

<%@ Page language="C #"autoeventwireup="true"codefile="Login.aspx.cs"inherits="Login"%><! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">"http://www.w3.org/1999/xhtml">"Server"> <title></title> <script type="Text/javascript">function Checksubmit () {varuser = document.getElementById ("txtname"); varReg =/^\s*$/; if(Reg.test (user.value)) {alert ("Please enter the user name! ");                User.focus (); return false; }            varPWD = document.getElementById ("txtpwd"); if(Reg.test (pwd.value)) {alert ("Please enter your password! ");                Pwd.focus (); return false; }            return true; }        </script>"Form1"runat="Server">Login Name:<asp:textbox id="txtname"runat="Server"></asp:TextBox>Password:<asp:textbox id="txtpwd"runat="Server"></asp:TextBox> <asp:button id="Btn_login"runat="Server"text="Login"onclick="Btn_login_click"onclientclick="return Checksubmit ()"/> </form></body>Login.aspx

Background. cs file

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data; Public Partial classlogin:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {    }    protected voidBtn_login_click (Objectsender, EventArgs e) {        stringName =TxtName.Text.Trim (); stringPWD =TxtPwd.Text.Trim (); if(Name = =string. Empty) {MessageBox.Show ( This,"Please enter the user name! "); return; }        if(pwd = =string. Empty) {MessageBox.Show ( This,"Please enter your password! "); return; }            if(Name = ="Test"&& pwd = ="123456") {RESPONSE.COOKIES.ADD (NewHttpCookie ("Comid"," -")); response.cookies["Comid"]. Expires = DateTime.Now.AddDays ( -);//set expiration time, 30 daysResponse.Redirect ("Get cookies.aspx"); }        Else{MessageBox.Show ( This,"User name password is wrong! "); }            }        }

Get cookies.aspx page Background

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data; Public Partial classGet cookies:system.web.ui.page{ Public stringComid =""; protected voidPage_Load (Objectsender, EventArgs e) {        if(request.cookies["Comid"] !=NULL&& request.cookies["Comid"]. Value! ="") {Comid= request.cookies["Comid"]. Value;//get the comid deposited} MessageBox.Show ( This,"Login id="+comid); }}


When I log in the time deposit 100, visit Login.aspx will jump to get the cookie page, prompt login id=100, as follows:

Cookies in the manner in which the state is stored in the program

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.