Session (Personal arrangement) for transferring values between ASP. NET pages, asp. netsession

Source: Internet
Author: User

Session (Personal arrangement) for transferring values between ASP. NET pages, asp. netsession

Session

In ASP. NET, Session indicates the Session between the client (Goggle, Firefox, IE, etc.) and the server, which is used to store specific Session information. Specifically, Session is used to store specific user information. When a client sends a request to the server, such as the Login User ID, the server receives the request, the server Session generates a SessionID related to the login user, and return SessioID to the client (Goggle, Firefox, IE, etc.). At the beginning of the new session, the server stores SessionID as a cookie in the user's browser.

Session operations are similar to Application operations, which act on individual users. Therefore, excessive storage will exhaust server memory resources.

Why is Session introduced? As we all know, because http is a stateless protocol, Session is making up for this defect. Of course, the role of Session is far more than that, and we will not discuss it here.

Advantages: 1. Simple to use, not only can pass simple data types, but also can pass objects.
2. The data size is unlimited.

Disadvantages: 1. storing a large amount of data in the Session variable will consume a lot of server resources.

2. easy to lose. (Session loss when the IIS server is restarted)

Ps: You can configure to store Session data in the SQL Server database. To perform this configuration, the programmer must first prepare the SQL Server data Server and then run it. NET built-in Installation Tool installation status database. This method remains after the server is suspended and restarted, because it is stored in the memory and disk.

Usage: 1. Create the Name and Value you need to pass in the Code on the Source Page to construct the Session variable: Session ["Name"] = "Value (Or Object )";

2. The code on the target page uses the Session variable to retrieve the passed value. Result = Session ["Nmae"]

Note: A session can be destroyed when it is not used. The destroy method is: Clear 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> 

(2) a. aspx. cs

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace WebApplication {public partial class a: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {} protected void button#click (object sender, EventArgs e) {Session ["name"] = Label1.Text; // put the label1 value into the Session. timeout = 1; // set the duration of the Session to one minute. After one minute, the Session will fail to respond. redirect ("B. aspx ");}}}

(3) B. aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="b.aspx.cs" Inherits="WebApplication.b" %><!DOCTYPE html>

(4) B. aspx. cs

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace WebApplication{    public partial class b : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            Label1.Text = Session["name"].ToString();        }    }}

(5) Run page a and click "Button" to go to page B. If you refresh the page one minute later, an error "object is not instantiated" will be reported. This is normal because the Session has expired!

                         

Ps: This article is based on my understanding on the Internet. If your rights and interests are inadvertently violated, please contact me.

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.