Skip to main content

Accessing View State of one page on other ASP.Net Page


There was one question always occurs, can we access viewstate one page to other page? And answer is Yes

“Is it possible to access the ViewState variable of one page on another page?”

We can access the viewstate variables across pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user to other page. If Response.redirect is used, then ViewState cannot be accessed across pages.

Here I have two pages Default.aspx and Default2.aspx.I have one Textbox and button control in first page (Default.aspx), after submit button it will be redirect to other page (Default2.aspx) were u get first page viewstate

Default.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" Style="height: 26px" />
    </div>
    </form>
</body>
</html>

Code Behind
protected void Page_Load(object sender, EventArgs e)
{
}
public StateBag ReturnViewState()
{
    return ViewState;
}
protected void Button1_Click(object sender, EventArgs e)
{
    ViewState["test"] = TextBox1.Text.ToString();
    Server.Transfer("Default2.aspx");
}

Defualt2.aspx
protected void Page_Load(object sender, EventArgs e)
{
    StateBag objStateBag = null;
    if (PreviousPage != null)
    {
        Object objPreviousPage = (Object)PreviousPage;
        System.Reflection.MethodInfo objMethod = objPreviousPage.GetType().GetMethod("ReturnViewState");
        objStateBag = (StateBag)objMethod.Invoke(objPreviousPage, null);
    }
    Response.Write("First Page Textbox value : " + objStateBag["test"].ToString());
}
Suppose we entered “Viewstate testing” in first page text box, 

Output

First Page Textbox value : Viewstate testing
Download code here

Comments

  1. Read all the information that i've given in above article. It'll give u the whole idea about it.
    Best Devops Training in pune
    Data science training in Bangalore

    ReplyDelete
  2. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

    ReplyDelete
  3. Wonderful article, very useful and well explanation. Your post is extremely incredible. I will refer this to my candidates...
    Python Online training
    python Training in Chennai
    Python training in Bangalore

    ReplyDelete
  4. This is very good content you share on this blog. it's very informative and provide me future related information.
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  5. Hats off to your presence of mind...I really enjoyed reading your blog. I really appreciate your information which you shared with us.

    Looking for Training Institute in Bangalore , India. Softgen Infotech is the best one to offers 85+ computer training courses including IT software course in Bangalore, India. Also it provides placement assistance service in Bangalore for IT.

    ReplyDelete
  6. Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me. is on boom these days and offering services like these is going to help you run your business perfectly. Thanks thanks for ur efforts
    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  7. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.

    ReplyDelete
  8. Amazing web journal. I appreciated perusing your articles. This is really an incredible perused for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome!
    data scientist training and placement

    ReplyDelete
  9. Extraordinary post I should state and a debt of gratitude is in order for the data. Instruction is unquestionably a clingy subject. Be that as it may, is still among the main subjects within recent memory. I value your post and anticipate more.

    ReplyDelete
  10. Your work is very good and I appreciate you and hopping for some more informative posts
    data scientist certification malaysia

    ReplyDelete
  11. This post is very simple to read and appreciate without leaving any details out. Great work!
    data scientist training in hyderabad

    ReplyDelete
  12. Thanks for this insightful post on accessing ViewState across pages! Your explanations are clear and the examples are very helpful. It’s great to see practical solutions for common challenges in ASP.NET. Looking forward to more tips and tricks.
    Data Science Courses in Brisbane

    ReplyDelete
  13. Thank you for this well-rounded post on accessing view state of one page. You shared some perspectives I hadn’t considered before, which really expanded my understanding. Great content.
    https://iimskills.com/data-science-courses-in-westminster/


    ReplyDelete

  14. This explanation effectively clarifies how ViewState can be shared between pages in ASP.NET using techniques like Cross Page Posting or Server.Transfer. It's a useful approach for maintaining state across pages, which is often necessary in web applications. The provided code snippets demonstrate a clear implementation of this concept with a straightforward example, showing how data can be passed from one page to another seamlessly. This can help developers manage user input more effectively without needing to resort to other state management techniques. Overall, a great resource for understanding ViewState in ASP.NET! Data science courses in Gurgaon

    ReplyDelete
  15. Thank you for sharing such valuable insights! Great post! I always appreciate your attention
    Data science courses in Dubai

    ReplyDelete
  16. This is a great explanation of how to access ViewState across pages using Server.Transfer and Cross Page Posting! Many developers are often unsure if it’s possible to share ViewState data between pages, and this article clears up the confusion. Data science courses in Visakhapatnam

    ReplyDelete
  17. "Such a helpful resource for anyone considering data science training! If you're in Brighton, the Data Science courses in Brighton are a great place to explore different learning paths. A fantastic way to take the first step toward a data science career."

    ReplyDelete
  18. "I really enjoyed reading this! Anyone in Kochi interested in data science should definitely look into the Data Science courses in Kochi for an excellent start to their learning path."

    ReplyDelete
  19. Managing ViewState across different pages in ASP.NET requires alternative methods, such as using session variables or query strings. For sensitive data, storing the data in a session is a safer and more reliable approach.

    Data science courses in Pune

    ReplyDelete
  20. Thanks for the helpful guide! It’s a great solution for accessing the ViewState from another page. Really appreciate the clear explanation!
    Data science Courses in Canada

    ReplyDelete
  21. Great explanation! You made accessing View State across pages in ASP.NET so clear and easy to follow. Thanks for sharing.
    Data science Courses in Sydney

    ReplyDelete
  22. "Great explanation! I appreciate how you’ve broken down the process of accessing view state across pages in ASP.NET. The example is clear and provides a practical approach to handling scenarios where state management is crucial. It’s helpful for understanding the limitations and potential solutions in such use cases. Thanks for sharing this detailed guide!"
    Data science courses in the Netherlands

    ReplyDelete
  23. "I love how the author breaks down the information in such an easy-to-follow way. Highly recommend!
    Data science Courses in London

    ReplyDelete
  24. This post on accessing view state from one page to another in ASP.NET is a real time-saver! The examples you provided made the concept clear and easy to implement. I can see how this will be helpful for managing user data across pages. Thanks for the great tips
    Data science courses in Glasgow

    ReplyDelete
  25. Learn how to access the view state of one page on another in .NET with this practical guide.

    Data science courses in France

    ReplyDelete

Post a Comment

Popular posts from this blog

Connected and disconnected architecture in ADO.Net with Example

Connected Architecture of ADO.NET The architecture of ADO.net, in which connection must be opened to access the data retrieved from database is called as connected architecture. Connected architecture was built on the classes connection, command, datareader and transaction.  Connected architecture is when you constantly make trips to the database for any CRUD (Create, Read, Update and Delete) operation you wish to do. This creates more traffic to the database but is normally much faster as you should be doing smaller transactions. Disconnected Architecture in ADO.NET The architecture of ADO.net in which data retrieved from database can be accessed even when connection to database was closed is called as disconnected architecture. Disconnected architecture of ADO.net was built on classes connection, dataadapter, commandbuilder and dataset and dataview. Disconnected architecture is a method of retrieving a r...

ASP.Net AJAX Interview Questions

What is Ajax? Ajax means " Asynchronous Javascript and XM L". Ajax represents a set of commonly used techniques, like HTML/XHTML, CSS, Document Object Model(DOM), XML/XSLT, Javascript and the XMLHttpRequest object, to create RIA's (Rich Internet Applications). ASP.NET AJAX update only a specified portion of data without refreshing the entire page. The ASP.NET AJAX works with the AJAX Library that uses object-oriented programming (OOP) to develop rich Web applications that communicate with the server using asynchronous postback. What are the different controls of ASP.NET AJAX? ASP.NET AJAX includes the following controls: ScriptManager ScriptManagerProxy UpdatePanel UpdateProgress Timer Describe the technology that makes up AJAX?   AJAX is based on the following web standards: XMLHttpRequest Object JavaScript HTML/DHTML DOM XML CSS What role does the ScriptManager play?   The ScriptManager manages all ASP.Net AJAX resour...

HTTPHandler and HTTPModule in ASP.NET

If you want to implement pre-processing logic before a request hits the IIS resources. For instance you would like to apply security mechanism, URL rewriting, filter something in the request, etc. ASP.NET has provided two types of interception HttpModule and HttpHandler .   The web server examines the file name extension of the requested file, and determines which ISAPI extension should handle the request. Then the request is passed to the appropriate ISAPI extension.  For Example When an .aspx page is requested it is passed to ASP.Net page handler. Then Application domain is created and after that different ASP.Net objects like Httpcontext, HttpRequest, HttpResponse. HTTPModule: -    It's just like a filter. The Modules are called before and after the handler executes . -    HTTP Modules are objects which also participate the pipeline but they work before and after the HTTP Handler does its job, and produce additional serv...