国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在ASP.NET中的變量數值管理--看了這個我基本上對原來的REQUEST.FORM的方法傳遞變量絕

2024-07-10 13:08:10
字體:
來源:轉載
供稿:網友
國內最大的酷站演示中心!
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!!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江城| 鸡西市| 福鼎市| 云和县| 三穗县| 青田县| 蓝田县| 循化| 阳新县| 河曲县| 盖州市| 惠水县| 同德县| 新民市| 方城县| 镇平县| 万载县| 平和县| 乐清市| 江源县| 沧源| 桂平市| 铜梁县| 龙江县| 镇雄县| 江北区| 崇明县| 新和县| 长沙市| 驻马店市| 张家川| 方正县| 临海市| 界首市| 通州市| 巴林左旗| 井研县| 长岭县| 南平市| 西平县| 清涧县|