Access View State Value In Another Page

Introduction

We all know a view state value cannot be accessed in a different page because the life span of a view state object is within the page only. That means directly we can’t access a view state value in another page but indirectly it is possible.

These are some ways using which we can access a view state value in another page.

  1. Using Query String
  2. Using Session State
  3. Using cookies
  4. Using cross Page Postback

Let us go with all the above cases one by one with some real time example. For example, I am taking C# Corner websites article section.

When you open an article you will find some fields like Page Views, Likes and Downloads. Here's the image:

run
                                             Figure 1: Like Page Views

In the above screenshot there are four fields such as:

  1. Download Source code
  2. Page Views
  3. Likes
  4. View Details
  • When you click on Download Source code link it will increment the counter by one, that means it will count number of people downloading that article.

  • When you Refresh/Reload the page it will increment the counter by one, that means it will count number of people reading that article.

  • When you click on like button it will increment the counter by one, that means it will count number of people like that page.

  • And when you click on ViewDetails link button, it will redirect you to another page showing all the details.

Let us design our page like the above screenshot.

  • Open VS, New Project, then ASP.NET Empty Website. Name it “CrossPageViewState”.

  • Right click on project head, Add, WebForm, then name it “TestViewState”.

    Write the following code inside it.

TestViewState.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestViewState.aspx.cs" Inherits="CrossPageViewState.TestViewState" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5. <head runat="server">  
  6.     <title></title>  
  7. </head>  
  8.   
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.         <asp:Image ID="img1" runat="server" ImageUrl="~/Images/c-sharp-corner-c-corner-logo-icon.jpg" ToolTip="CSharpCorner" />  
  12.         <p style="height:30px; width:100%; background-color:blue; color:white; text-align:center; font-size:18px; font-family:Tahoma"> <a href="#" style="color:white">Technologies</a> | <a href="#" style="color:white">Answer</a> | <a href="#" style="color:white">Blog</a> | <a href="#" style="color:white">Videos</a> | <a href="#" style="color:white">InterView</a> | <a href="#" style="color:white">Books</a> | <a href="#" style="color:white">News</a> | <a href="#" style="color:white">Carrer</a> </p>  
  13.         <div align="center">  
  14.             <table>  
  15.                 <tr style="text-align:center">  
  16.                     <td> <b style="color:blue; font-size:30px">Article:-</b> </td>  
  17.                     <td>  
  18.                         <h1>Cross Page ViewState</h1> </td>  
  19.                 </tr>  
  20.             </table>  
  21.             <asp:LinkButton ID="lnkDownload" runat="server" Text="Download SourceCode" OnClick="lnkDownload_Click"></asp:LinkButton>    
  22.             <asp:Label ID="lblDownload" runat="server" Font-Bold="true" Font-Size="40px" />         
  23.             <asp:Image ID="imgView1" runat="server" ImageUrl="~/Images/view.jpg" ToolTip="View" />    
  24.             <asp:Label ID="lblView" runat="server" Font-Bold="true" Font-Size="40px" />          
  25.             <asp:ImageButton ID="imgLike" runat="server" ImageUrl="~/Images/like.png" ToolTip="likes" OnClick="imgLike_Click" />    
  26.             <asp:Label ID="lblLike" runat="server" Font-Bold="true" Font-Size="40px" />               
  27.             <asp:LinkButton ID="lnkViewDetails" runat="server" Text="View Details" OnClick="lnkViewDetails_Click" /> <br /><br /><br />  
  28.             <p>********************<b> Article Content </b>*********************** </p>  
  29.         </div>  
  30.     </form>  
  31. </body>  
  32.   
  33. </html>  
Now we will store all the field values of the above form in view state and will access it in some other page. To do this add the following code view to the above webform. We can do this task in so many ways. Firstly, we will use Query string approach to do this.

1. Using Query String

TestViewState.aspx.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. namespace CrossPageViewState  
  8. {  
  9.     public partial class TestViewState: System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.             if (!Page.IsPostBack)  
  14.             {  
  15.                 ViewState["Download"] = 0;  
  16.                 lblDownload.Text = ViewState["Download"].ToString();  
  17.                 View();  
  18.                 //ViewState["View"] = 1;  
  19.                 //lblView.Text = ViewState["View"].ToString();  
  20.                 ViewState["Like"] = 0;  
  21.                 lblLike.Text = ViewState["Like"].ToString();  
  22.             }  
  23.         }  
  24.         public void View()  
  25.         {  
  26.             if (Application["View"] == null)  
  27.             {  
  28.                 Application["View"] = 1;  
  29.             }  
  30.             else  
  31.             {  
  32.                 int Temp = (int) Application["View"];  
  33.                 Temp += 1;  
  34.                 Application["View"] = Temp;  
  35.             }  
  36.             lblView.Text = Application["View"].ToString();  
  37.         }  
  38.         protected void lnkDownload_Click(object sender, EventArgs e)  
  39.         {  
  40.             int count = (int) ViewState["Download"];  
  41.             count += 1;  
  42.             ViewState["Download"] = count;  
  43.             lblDownload.Text = ViewState["Download"].ToString();  
  44.         }  
  45.         protected void imgLike_Click(object sender, ImageClickEventArgs e)  
  46.         {  
  47.             int count = (int) ViewState["Like"];  
  48.             count += 1;  
  49.             ViewState["Like"] = count;  
  50.             lblLike.Text = ViewState["Like"].ToString();  
  51.         }  
  52.         protected void lnkViewDetails_Click(object sender, EventArgs e)  
  53.         {  
  54.             int download = (int) ViewState["Download"];  
  55.             int like = (int) ViewState["Like"];  
  56.             Response.Redirect("ViewDetails.aspx?download=" + download + "&like=" + like);  
  57.         }  
  58.     }  
  59. }  
Now add another webform to the project and name it “ViewDetails.aspx”. Write the following code inside it.

ViewDetails.aspx
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3.   
  4.     <head runat="server">  
  5.         <title></title>  
  6.     </head>  
  7.   
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <p style="height:45px; width:100%; background-color:yellowgreen; color:white; text-align:center; font-size:30px; font-family:Tahoma">Article Details</p> <br /><br />  
  12.                 <div align="center">  
  13.                     <asp:Label ID="lblArticleStatus" runat="server" /> </div>  
  14.             </div>  
  15.         </form>  
  16.     </body>  
  17.   
  18. </html>  
ViewDetails.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. namespace CrossPageViewState  
  8. {  
  9.     public partial class ViewDetails: System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.             if (Request.QueryString["download"] != null && Request.QueryString["like"] != null)  
  14.             {  
  15.                 lblArticleStatus.Text = "<table border='1' width=40%><tr><th>Title</th><td>Cross Page ViewState</td><tr><th>Page Views</th><td>" + Application["View"] + "</td></tr><tr><th>Likes</th><td>" + Request.QueryString["like"].ToString() + "</td></tr><tr><th>Downloads</th><td>" + Request.QueryString["download"].ToString() + "</td></tr></table>";  
  16.             }  
  17.             else  
  18.             {  
  19.                 Response.Redirect("TestViewState.aspx");  
  20.             }  
  21.         }  
  22.     }  
  23. }  
Now go to TestViewState.aspx page and run it by pressing ctrl+f5 .you will find pageview=1, downloads=0, like=0.now how many times you will refresh the page the pageview value will be increase by one. When you click on download/like button the corresponding value will increase.

Finally click on ViewDetails link, when you click on this button it will redirect you to ViewDetails page by appending all the viewstate values on the url.

Article detail
                                             Figure 2: Showing all the article details

2. Using Session State

Using session state we can transfer view state value from one page to another page.

To store value in session state use the following technique,
  1. int download = (int)ViewState["Download"];  
  2. int like = (int)ViewState["Like"];  
  3. Session["download"] =download;  
  4. Session["like"] = like;  
To retrieve:
  1. lblArticleStatus.Text = "<table border='1' width=40%><tr><th>Title</th><td>Cross Page ViewState</td><tr><th>Page Views</th><td>" + Application["View"] + "</td></tr><tr><th>Likes</th><td>" + Session ["like"].ToString() + "</td></tr><tr><th>Downloads</th><td>" + Session ["download"].ToString() + "</td></tr></table>";  
3. Using Cookies

Using cookies also we can transfer the view state values from one page to another.

To store value in cookie:
  1. HttpCookie objCookie1 = new HttpCookie("download");  
  2. objCookie1.Value = ViewState["Download"];  
  3. HttpCookie objCookie1 = new HttpCookie("like");  
  4. objCookie1.Value = ViewState["Like"];  
  5. Response.Cookies.Add(objCookie1);  
  6. Response.Cookies.Add(objCookie2);  
To retrieve from cookie:
  1. if (Request.Cookies["download"] != null && Request.Cookies["like"] != null)  
  2. {  
  3.    lblArticleStatus.Text = "<table border='1' width=40%><tr><th>Title</th><td>Cross Page ViewState</td><tr><th>Page Views</th><td>" + Application["View"] + "</td></tr><tr><th>Likes</th><td>" + Request.cookies["like"].value + "</td></tr><tr><th>Downloads</th><td>" + Request.cookies["download"].value + "</td></tr></table>";  
  4. }  

Read more articles  on View State: