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

Let us design our page like the above screenshot.

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. <head runat="server">
  5. <title></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <asp:Image ID="img1" runat="server" ImageUrl="~/Images/c-sharp-corner-c-corner-logo-icon.jpg" ToolTip="CSharpCorner" />
  10. <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>
  11. <div align="center">
  12. <table>
  13. <tr style="text-align:center">
  14. <td> <b style="color:blue; font-size:30px">Article:-</b> </td>
  15. <td>
  16. <h1>Cross Page ViewState</h1> </td>
  17. </tr>
  18. </table>
  19. <asp:LinkButton ID="lnkDownload" runat="server" Text="Download SourceCode" OnClick="lnkDownload_Click"></asp:LinkButton>
  20. <asp:Label ID="lblDownload" runat="server" Font-Bold="true" Font-Size="40px" />
  21. <asp:Image ID="imgView1" runat="server" ImageUrl="~/Images/view.jpg" ToolTip="View" />
  22. <asp:Label ID="lblView" runat="server" Font-Bold="true" Font-Size="40px" />
  23. <asp:ImageButton ID="imgLike" runat="server" ImageUrl="~/Images/like.png" ToolTip="likes" OnClick="imgLike_Click" />
  24. <asp:Label ID="lblLike" runat="server" Font-Bold="true" Font-Size="40px" />
  25. <asp:LinkButton ID="lnkViewDetails" runat="server" Text="View Details" OnClick="lnkViewDetails_Click" /> <br /><br /><br />
  26. <p>********************<b> Article Content </b>*********************** </p>
  27. </div>
  28. </form>
  29. </body>
  30. </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. <head runat="server">
  4. <title></title>
  5. </head>
  6. <body>
  7. <form id="form1" runat="server">
  8. <div>
  9. <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 />
  10. <div align="center">
  11. <asp:Label ID="lblArticleStatus" runat="server" /> </div>
  12. </div>
  13. </form>
  14. </body>
  15. </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: