Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2007, 05:17 AM
Authorized User
 
Join Date: Jun 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vishwjeet
Default What SaveViewState does?

HI All

I created a page with a text box and a button, both server side. When I enter some text inside the textbox and post the page back to server, what saveviewstate and loadviewstate should do?
On doing the same, LoadViewState is not firing. But when I override saveview state and enter dummy keys into the viewstate object, LoadViewState is fired.
Why LoadViewState is not fired when I changed the text in the textbox and posted back the page?

Any explanations?

__________________
- Vishu
 
Old June 20th, 2007, 08:22 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

LoadViewState is called during the initialization of a page postback. It restores the state of the controls as they were during the previous rendering. This is important to determine the events to fire (for example, if a drop down list was previously at index 0 and is now at index 5 the server knows the user changed the DDL item).

SaveViewState takes all the states of the controls and serializes them into the viewstate value emitted to the page.

You can add custom viewstate actions by overriding those methods, but you need to ensure you call the base class LoadViewState method before you try to access view state data and similarly you need to call the SaveViewState method after you do your modification otherwise your data won't be stored.

As to why it's not firing? That's hard to say. How are you determining it is not firing? Does the page and/or application have viewstate disabled?

-Peter
 
Old June 20th, 2007, 11:56 AM
Authorized User
 
Join Date: Jun 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vishwjeet
Default

Thanks for the clarification. The info was indeed just what I was looking for.


This is what I am doing in my codefile:

    protected override void LoadViewState(object savedState)
    {
        Response.Write("<br>This is Load View State");
    }

    protected override object SaveViewState()
    {
        Response.Write("<br>Inside Save View State");
        this.ViewState.Add("tbxPostBack",tbxPostBack.Text. ToString());

        return base.SaveViewState();
    }

When I remove ViewState.Add...I dont see "This is Load View State" in the output..So I can say that LoadViewState is not fired. Yes, I have set EnableViewState = "true" in the Page directive.
 
Old June 20th, 2007, 12:55 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Two things:

1) You are overriding LoadViewState but not calling it on the base class. This means the viewstate will never get loaded. You must add this as the first line in your override method:

base.LoadViewState(savedState);

2) I'm confused as to why you are explicitly storing a textbox value to viewstate. The control does that automatically assuming you have viewstate enabled at that control. Even without viewstate, a textbox control value should ALWAYS be persisted because it is part of the form post.

-Peter
 
Old June 20th, 2007, 01:14 PM
Registered User
 
Join Date: Sep 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Saves the state of the ObjectDataSource control.
 
Old June 20th, 2007, 01:47 PM
Authorized User
 
Join Date: Jun 2007
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vishwjeet
Default

Peter

View state will never get loaded but the overridden LoadViewMust be fired and the response.write should work. I have taken your point and added base.LoadViewState(savedState) in the beginning of the method.

I think my doubt is more related to the second point mentioned by you. I thaught that whenever the value in text box is changed it will modify the ViewState and Load state will get fired.

I tried what you explained in the previous post, I added a dropdown to my page and now the LoadViewState is fired everytime.

I read somewhere, .net 2.0 will not fire LoadViewState if SaveViewState does not enters something into the ViewState( I dont know how too loop through all the entries in View State) and thats why I manually entered something into it..just to check.

-Vishu









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.