Click here to Skip to main content
Click here to Skip to main content

Pages.Items Property

By , 11 Mar 2013
Rate this:
Please Sign up or sign in to vote.

Introduction 

I did not find a suitable article describing how Page.Items can help to transfer data from Page to another page. Of course, state management like: query strings, session, Cache object, Application and others can help us to do that.

Background 

Page.Items Property

This property gets a list of objects stored in the page context, but we will use it here to persist data from one ASP.NET page to another page.

Objects added to the Items property are available throughout the lifetime of the page, so you can add objects to the Items property in events early in the page life-cycle and access those objects in later events.

Page.Items vs. ViewState

ViewState helps to save data at the page during multiple posts back of the same page. On the contrary, Page.Items is refreshed with each page post-back.

Using the Code

The example code scenario is a login page that saves the user name and uses Server.Transfer to transfer it to another page, where it can be retrieved from the context of the previous page. Of course the value will be lost upon the first post back of the page.

In page Login.aspx the Page.Items property saves the user name in its dictionary after the user enters his name "Omar", password "secret", and clicks the login button . 

/Login.aspx.cs 

 

Login.aspx  

The next page, Default.aspx will use the context of the previous page to get the user name and display the it on the label. Therefore, you need to save the Page.Items value from the first page load in a session, view state or other preferred methodology. 

/Default.aspx.cs

 

You can use the button to cause a post-back and refresh the page, and you will notice that an exception will be thrown.

/Default.aspx

 

Resources

  • MSDN library
  • Practical experience

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Omar Adnan Isaid
Software Developer (Junior)
Jordan Jordan
Omar Adnan Isaid Junior developer interested to learn more about web development and meet experts in this field aims to add something unique to IT field.
http://omarpost.blogspot.com/

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 11 Mar 2013
Article Copyright 2013 by Omar Adnan Isaid
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid