在ASP.NET中的变量数值管理------看了这个我基本上对原来的REQUEST.FORM的方法传递变量绝望了

asp.net|request|变量

Web form pages are HTTP-Based, they are stateless, which means they don’t know whether the requests are all from the same client, and pages are destroyed and recreated with each round trip to the server, therefore information will be lost, therefore state management is really an issue in developing web applications

We could easily solve these problems in ASP with cookie, query string, application, session and so on. Now in ASP.NET, we still can use these functions, but they are richer and more powerful, so let’s dive into it.

Mainly there are two different ways to manage web page’s state: Client-side and Server-side.

1.Client-side state management :
There is no information maintained on the server between round trips. Information will be stored in the page or on the client’s computer.

A. Cookies.

A cookie is a small amount of data stored either in a text file on the client's file system or in-memory in the client browser session. Cookies are mainly used for tracking data settings. Let’s take an example: say we want to customize a welcome web page, when the user request the default web page, the application first to detect if the user has logined before, we can retrieve the user informatin from cookies:
[c#]
if (Request.Cookies[“username”]!=null)
lbMessage.text=”Dear “+Request.Cookies[“username”].Value+”, Welcome shopping here!”;
else
lbMessage.text=”Welcome shopping here!”;

If you want to store client’s information, you can use the following code:
[c#]
Response.Cookies[“username’].Value=username;

So next time when the user request the web page, you can easily recongnize the user again.

B. Hidden Field

A hidden field does not render visibly in the browser, but you can set its properties just as you can with a standard control. When a page is submitted to the server, the content of a hidden field is sent in the HTTP Form collection along with the values of other controls. A hidden field acts as a repository for any page-specific information that you would like to store directly in the page. Hidden field stores a single variable in its value property and must be explicitly added it to the page.
ASP.NET provides the HtmlInputHidden control that offers hidden field functionality.
[c#]
protected System.Web.UI.HtmlControls.HtmlInputHidden Hidden1;
//to assign a value to Hidden field
Hidden1.Value=”this is a test”;
//to retrieve a value
string str=Hidden1.Value;

Note: Keep in mind, in order to use hidden field, you have to use HTTP-Post method to post web page. Although its name is ‘Hidden’, its value is not hidden, you can see its value through ‘view source’ function.

C. View State

Each control on a Web Forms page, including the page itself, has a ViewState property, it is a built-in struture for automatic retention of page and control state, which means you don’t need to do anything about getting back the data of controls after posting page to the server.

Here, which is useful to us is the ViewState property, we can use it to save information between round trips to the server.
[c#]
//to save information
ViewState.Add(“shape”,”circle”);
//to retrieve information
string shapes=ViewState[“shape”];

Note: Unlike Hidden Field, the values in ViewState are invisible when ‘view source’, they are compressed and encoded.

D. Query Strings

Query strings provide a simple but limited way of maintaining some state information.You can easily pass information from one page to another, But most browsers and client devices impose a 255-character limit on the length of the URL. In addition, the query values are exposed to the Internet via the URL so in some cases security may be an issue.
A URL with query strings may look like this:

http://www.examples.com/list.aspx?categoryid=1&productid=101

When list.aspx is being requested, the category and product information can be obtained by using the following codes:
[c#]
string categoryid, productid;
categoryid=Request.Params[“categoryid”];
productid=Request.Params[“productid”];

Note: you can only use HTTP-Get method to post the web page, or you will never get the value from query strings.

2. Server-side state management:
Information will be stored on the server, it has higher security but it can use more web server resources.

A. Aplication object

The Application object provides a mechanism for storing data that is accessible to all code running within the Web application, The ideal data to insert into application state variables is data that is shared by multiple sessions and does not change often.. And just because it is visible to the entire application, you need to used Lock and UnLock pair to avoid having conflit value.

[c#]

Application.Lock();

Application[“mydata”]=”mydata”;

Application.UnLock();

B. Session object

Session object can be used for storing session-specific information that needs to be maintained between server round trips and between requests for pages. Session object is per-client basis, which means different clients generate different session object.The ideal data to store in session-state variables is short-lived, sensitive data that is specific to an individual session.

Each active ASP.NET session is identified and tracked using a 120-bit SessionID string containing URL-legal ASCII characters. SessionID values are generated using an algorithm that guarantees uniqueness so that sessions do not collide, and SessionID’s randomness makes it harder to guess the session ID of an existing session.
SessionIDs are communicated across client-server requests either by an HTTP cookie or a modified URL, depending on how you set the application's configuration settings. So how to set the session setting in application configuration? Ok, let’s go further to look at it.

Every web application must have a configuration file named web.config, it is a XML-Based file, there is a section name ‘sessionState’, the following is an example:

<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;user id=sa;password=" cookieless="false" timeout="20" />

‘cookieless’ option can be ‘true’ or ‘false’. When it is ‘false’(default value), ASP.NET will use HTTP cookie to identify users. When it is ‘true’, ASP.NET will randomly generate a unique number and put it just right ahead of the requested file, this number is used to identify users, you can see it on the address bar of IE:

http://localhost/Management/(2yzakzez3eqxut45ukyzq3qp)/Default.aspx

Ok, it is further enough, let is go back to session object.
[c#]
//to store information
Session[“myname”]=”Mike”;
//to retrieve information
myname=Session[“myname”];

C. Database

Database enables you to store large amount of information pertaining to state in your Web application. Sometimes users continually query the database by using the unique ID, you can save it in the database for use across multiple request for the pages in your site.

Summary

ASP.NET has more functions and utilities than ASP to enable you to manage page state more efficient and effective. Choosing among the options will depand upon your application, you have to think about the following before making any choose:

How much information do you need to store?
Does the client accept persistent or in-memory cookies?
Do you want to store the information on the client or server?
Is the information sensitive?
What kind of performance experience are you expecting from your pages?
Client-side state management summary

Method
Use when

Cookies
You need to store small amounts of information on the client and security is not an issue.

View state
You need to store small amounts of information for a page that will post back to itself. Use of the ViewState property does supply semi-secure functionality.

Hidden fields
You need to store small amounts of information for a page that will post back to itself or another page, and security is not an issue.

Note   You can use a hidden field only on pages that are submitted to the server.

Query string
You are transferring small amounts of information from one page to another and security is not an issue.

Note   You can use query strings only if you are requesting the same page, or another page via a link.

Server-side state management summary

Method
Use when

Application state object
You are storing infrequently changed, application-scope information that is used by many users, and security is not an issue. Do not store large quantities of information in an application state object.

Session state object
You are storing short-lived information that is specific to an individual session, and security is an issue. Do not store large quantities of information in a session state object. Be aware that a session state object will be created and maintained for the lifetime of every session in your application. In applications hosting many users, this can occupy significant server resources and affect scalability.

Database support
You are storing large amounts of information, managing transactions, or the information must survive application and session restarts. Data mining is a concern, and security is an issue.

Developing ASP.NET programme is really funny, once you jump into it, you can feel the power of it. Next time let us talk about another topic: Cache. Enjoy .Net!!

时间: 2025-01-31 06:01:49

在ASP.NET中的变量数值管理------看了这个我基本上对原来的REQUEST.FORM的方法传递变量绝望了的相关文章

在ASP.NET中的变量数值管理--看了这个我基本上对原来的REQUEST.FORM的方法传递变量绝

Web form pages are HTTP-Based, they are stateless, which means they don't know whether the requests are all from the same client, and pages are destroyed and recreated with each round trip to the server, therefore information will be lost, therefore

在ASP.NET中实现Url Rewriting

asp.net 概要 分析如何使用微软提供的ASP.NET来对动态产生的URL地址进行网址重写. 网址重写是实现一种截取网址请求并将其进行处理后重新指向到一个指定的网址的过程.作者本人在对各种实现网址重写的技术进行研究和探讨后得出的经验和方法,希望能对您有所帮助. 内容简介 稍微花点时间看一看你做的网站里头的URL地址,你看到类似这样的地址吗http://yoursite.com/info/dispEmployeeInfo.aspx?EmpID=459-099&type=summary ?也许你

秒懂ASP.NET中的内置对象

       上篇博客,小编主要简单的介绍了一下ASP.NET中的控件,这篇博客,小编主要简单总结一下ASP.NET中的内置对象,七个内置对象分别是:Request.Response.Application.Cookies.Session.Server.Trace.这些对象使得用户更容易收集通过浏览器请求发送的信息.相应浏览器以及存储用户信息,以实现其他特定的状态管理和页面信息的传递,首先,我们来看下面一张图:                 接下来,小编主要从概述,基本语法,常用属性和方法以及

在ASP应用中如何限制同一表单被多次提交!!!!好东西

  在Internet上我们每天都会遇到数不清的表单,也看到其中大部分并没有限制用户多次提交同一个表单.缺乏这种限制有时候会产生某些预料不到的结果,如重复订阅邮件服务或重复投票等. 本文介绍在ASP应用中防止用户在当前会话期间多次提交同一表单的一个简单方法.它主要由四个子程序组成,在较为简单的应用场合,你只要将这些代码放在包含文件中直接引用即可:对于那些较为复杂的环境,我们在文章的最后给出一些改进建议. 一.基本工作过程 下面我们依次讨论这四个子程序. (一)初始化 这里我们要在Session对

在ASP应用中如何限制同一表单被多次提交----之小解

                在ASP应用中如何限制同一表单被多次提交       在Internet上我们每天都会遇到数不清的表单,也看到其中大部分并没有限制用户多次提交同一个表单.缺乏这种限制有时候会产生某些预料不到的结果,如重复订阅邮件服务或重复投票等. 本文介绍在ASP应用中防止用户在当前会话期间多次提交同一表单的一个简单方法.它主要由四个子程序组成,在较为简单的应用场合,你只要将这些代码放在包含文件中直接引用即可:对于那些较为复杂的环境,我们在文章的最后给出一些改进建议. 一.基本工

在ASP应用中如何限制同一表单被多次提交

在Internet上我们每天都会遇到数不清的表单,也看到其中大部分并没有限制用户多次提交同一个表单.缺乏这种限制有时候会产生某些预料不到的结果,如重复订阅邮件服务或重复投票等.    本文介绍在ASP应用中防止用户在当前会话期间多次提交同一表单的一个简单方法.它主要由四个子程序组成,在较为简单的应用场合,你只要将这些代码放在包含文件中直接引用即可:对于那些较为复杂的环境,我们在文章的最后给出一些改进建议.     一.基本工作过程     下面我们依次讨论这四个子程序.     (一)初始化  

ASP.NET中数据库数据导入Excel并打印(1)

asp.net|excel|打印|数据|数据库 众所周知,WEB上的打印是比较困难的,常见的WEB上打印的方法大概有三种:       1.直接利用IE的打印功能.一般来说,这种方法可以做些扩展,而不是单单的调用javascript:print()这样简单,比如,可以使用如下代码:      <OBJECT   id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0>   </OB

在ASP.NET中进行文件处理(1)

asp.net 一种程序设计语言,对文件处理的能力的大小往往也是一种语言功能大小的一种表现.在ASP.NET中,对于文件的处理主要是通过.Net FrameWork SDK中的一个System.IO的名称空间来实现了,在System.IO名称空间中定义了许多关于文件处理的类(class).方法和属性,这些类.方法和属性使得在很容易在ASP.NET中进行文件处理.由于在此名称空间中有定义了许多类.方法和属性,而这些不可能通过本文来一一介绍.本文主要结合例子来具体介绍对于文件处理的基本操作.   

|zyciis| ASP.NET中 我有A页面要传参到B页面,但这里不允许用URL传参 那有什么办法呢 谢谢

问题描述 1:Cokies这个不稳定2:Session这个占资源3:Appliction公共的这个这里不能用4:URL这个是最好的,但我这里不允许用这个不能在URL中出现参数大家有什么其他办法谢谢 解决方案 解决方案二:formpost解决方案三:formpost解决方案四:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-tra