Asp. NET page transfer of values between the session (personal grooming)

Source: Internet
Author: User

  Session

Session in ASP., the client (Goggle,firefox,ie, etc.) and server-side sessions are used to store specific session information and, to be precise, to store specific user information. When the client sends a request to the server, such as the login user ID, the server receives the request, the server side session generates a SessionID associated with the logged-in user, and returns the Sessioid to the client (Goggle,firefox,ie, etc.). At the beginning of a new session, the server stores SessionID as a cookie in the user's browser.

The session operation is similar to application, which acts on the user's personal, so excessive storage can result in exhaustion of the server's memory resources.

Why was the session introduced? As you know, because HTTP is a stateless protocol, the session is making up for this flaw. Of course, the role of the session far more than these, here is not much discussion.

  Pros : 1. Simple to use, not only to pass simple data types, but also to pass objects.
2. The size of the data volume is unlimited.

  cons : 1. Storing large amounts of data in session variables consumes more server resources.

2. Easy to lose. (Session lost when restarting IIS server)

  PS: can be configured to store session data in the SQL Server database, in order to make such a configuration, the programmer first need to prepare the SQL Server data server, and then run. NET comes with the Install Tool installation state database. This is still the case after the server hangs up and restarts because he is stored in memory and on disk.

  How to use : 1. Create the name and value you need to pass in the code of the source page construct the Session variable: session["name"]= "Value (Or Object)";

2. The code in the destination page uses the session variable to remove the passed value. Result = session["Nmae"]

  Note : The session can be destroyed when not used, the method is: Clear one: Session.remove ("Session name"); Clear all: Session.clear ();

(1) a.aspx

<%@ Page language="C #"autoeventwireup="true"Codebehind="A.aspx.cs"inherits="Webapplication.a"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div> <asp:label id="Label1"runat="Server"text="Zhang June Bao"></asp:Label> <asp:button id="Button1"runat="Server"text="Button"onclick="Button1_Click"/> </div> </form></body>

(2) A.aspx.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacewebapplication{ Public Partial classA:system.web.ui.page {protected voidPage_Load (Objectsender, EventArgs e) {        }        protected voidButton1_Click (Objectsender, EventArgs e) {session["name"] =Label1.Text;            //Put the value of Label1 into session Session.Timeout=1;//Set session action time is one minute, the session expires after one minuteResponse.Redirect ("b.aspx"); }    }}

(3) b.aspx

<%@ Page language="C #"autoeventwireup="true"Codebehind="B.aspx.cs"inherits="webapplication.b"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div> <asp:label id="Label1"runat="Server"text="Label"></asp:Label> </div> </form></body>

(4) B.aspx.cs

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacewebapplication{ Public Partial classB:system.web.ui.page {protected voidPage_Load (Objectsender, EventArgs e) {Label1.Text= session["name"].        ToString (); }    }}

(5) Run a page, click on the button to enter the B page, a minute after the page will be updated to error "Do not instantiate the object", normal, that is because the session has expired!

                         

PS: This article is my reference to the online content plus their own understanding of integration, such as inadvertently violated your rights and interests, please contact me.

Asp. NET page transfer of values between the session (personal grooming)

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.