Asp. NET State Management IV (staging state httpcontext.items)

Source: Internet
Author: User
Tags error handling http request httpcontext

Asp. NET provides a class System.Web.HttpContext that is used to represent the context, which has a property items

Staging state is the use of the Httpcontext.items attribute to store data

The Httpcontext.items attribute in MSDN explains this: Gets the key values that can be used to organize and share data between IHttpModule and IHttpHandler during the HTTP request process

The Httpcontext.items property can hold any type of data, regardless of what data is stored in this property, will be automatically cleared at the end of the request processing, this is the staging state, the data storage time is very short.

For example:

We have a page a.aspx, there is a button id:submit, click the button to turn the page to b.aspx page public
void Submit_click (Object sender, EventArgs E)
{
SqlConnection myconnection = new SqlConnection ("server= (local) netsdk;database=pubs;"  Trusted_connection=yes ");
SqlDataAdapter mycommand = new SqlDataAdapter ("select * from Authors", myconnection);
DataSet ds = new DataSet ();
Mycommand.fill (ds, "Authors");
Put the data into the temporary storage
context.items["MyData"] =ds;
Server.Transfer (b.aspx);
}
Public
void Page_Load (Object sender, EventArgs E)
{
if (!) in b.aspx page.  IsPostBack)
{
//Obtain staging Data
DataSet ds= (DataSet) context.items["MyData"];
Additional data Procedure
}
}

We also saw the use of this feature in Ibuyspyportal:

The query string contains the Tabindedx and TabID parameters of the tab that is being requested. This information is used throughout the process of processing the request to filter the data to be displayed to the user.

http://www.ibuyspyportal.com/DesktopDefault.aspx?tabindex=1&tabid=2

To use a query string value, you need to first make sure that it is a valid value, and if not, do some error handling. It's not a bunch of code, but do you really want to copy it in every page and component that uses that value? Of course not! In the Portal example, even more is involved, because once we know tabid, we can preload other information.

Related Article

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.