Built-in object session, Cookic,application,viewstate

Source: Internet
Author: User
Tags server memory

first, built-in objects

(i) Response-Response Request Object
1. Definition: The Response object is used to dynamically respond to client referrals, control the information sent to the user, and dynamically generate responses. The response object only provides a data collection cookie that is used to write cookie values on the client. If the specified cookie does not exist, it is created. If it does exist, it will be updated automatically. The results are returned to the client browser.
2. Methods
(1) Redirect
Usage: Response.Redirect ("url")
The role is to redirect the server end multiplicity to another Web page
(2) Write
Function: Send to the client a variety of data that the browser can handle, including: HTML code, scripting, etc.
Usage: Response.Write ("") input string is displayed in interface, input <script>alert (') </script> popup dialog box
(ii) Request-Gets the requested object
request["Key"]-the string type is returned by default
To get the value passed in, the request object interacts with the client, collects the client's form, Cookies, hyperlinks, or collects server-side environment variables.

(iii) Session relative global object

Transfer values between different browsers, such as banks and other sites to secure the user name password stored in the session. Each computer accesses the server, it will be a separate set of Session,key values are the same, but the content is different.

Assignment: session["key"] = value;

In another page value: first to determine whether session["key" is not NULL, not the value, otherwise directed to the original page

String a = session["key"];

Advantages and Disadvantages

Session is very safe, because it exists in the server's memory, fetch the data is also the fastest; however, do not abuse the session, because it will cause server memory overflow, the server crashes (in order to avoid the same account for a short period of large access, so set up an unclear verification code)
Do not enlarge things in the session, if not, it will cause the waste of resources.

Session life cycle: 20 minutes (when an account closes the browser, it will still be on the server for 20 minutes, each user will cause the page to refresh the operation will make this 20 minutes re-timer)

Many websites, especially when it comes to Moneyde, have to be re-logged after a long period of non-operation because of his life cycle.

PS: When a user closes the browser and then uses the browser to log in the same account, the connection is not the original connection, like a customer service call,

Use the same phone to dial the same customer service phone, the opposite answer the phone is not necessarily the same person.

(iv) Cookies

Assignment: response.cookies["key"]. Value = values;

Value: request.cookies["key"]. Value;

Compare with session:

The same point: each computer access to the server, will be a separate set of sessions or Cookies,key values are the same, but the content is not the same.

Different points: The session is saved in the server memory, the cookie is stored on the user's hard disk

The session is not persistent, and it is stored for 20 minutes without any action on the page, and the cookie can be long-lasting or non-durable, depending on the user

Set life cycle: response.cookies["key"]. Expires = DateTime.Now.AddDays (7);//Save 7 days

Question: If the Chinese language will be garbled

WORKAROUND: The input text is encoded in a specific encoding. And then decode it in this way

String a = Httputility.urlencode (value, System.Text.Encoding.GetEncoding ("Utf-8")); response.cookies["Key"]. Value = A; Response.Redirect ("default2.aspx");
if (request.cookies["user"]! = NULL)          decode {String a = Httputility.urldecode (request.cookies["key"]. Value, System.Text.Encoding.GetEncoding ("Utf-8")); Label1.Text = A;}

(v) Application, for example, the version number, everyone gets the same thing, the use of a small range.

It is a global object with only one object in the global
As long as it's created, everyone takes the same value.
Life cycle: Permanent
Save Location: Service side

Assignment value: application["key"]= value;
Value: Application ("key");

(vi) ViewState (Learn)

Can be understood as a case

Similar to the middle process, due to the Web page is not fixed, the click button will refresh, the control is assigned to the value of the ViewState save, the value before the refresh to save a special code. Refresh and assign a value to the control

Second, repeater command operation (not commonly used)

1. ItemCommand Event: All the controls that can trigger events in Repeater will trigger this event

Background creation: Repeater1.itemcommand + = in Page_Load, then double-click the TAB key to create

2, CommandName: Judging what button is clicked,

background call: E.commandname

3, CommandArgument: Trigger the event passed the primary key value data, placed in the face of the boundary value binding time to use single quotation marks!!!!!!

Background call: E.commandargument

Deleted as an example

   <asp:button id= "Button1" commandname= "Delete" commandargument=<% #Eval ("UserName")%> runat= "Server" text= " Delete "/>  //foreground code--------------------------------------------------------------------------------------background code protected void Page_Load (object sender, EventArgs e)    {        if (! IsPostBack)        {            Repeater1.datasource = new Usersdata (). Select ();            Repeater1.databind ();        }        Repeater1.itemcommand + = Repeater1_itemcommand;    }    void Repeater1_itemcommand (object source, RepeaterCommandEventArgs e)    {        if (E.commandname = = "Delete")        {            new Usersdata (). Delete (E.commandargument.tostring ());}}}    

Built-in object session, Cookic,application,viewstate

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.