Solved

Clearing ASP.NET Viewstate

Posted on 2004-09-26
12
8,902 Views
Last Modified: 2007-12-19
I would like to know is there any way to clear out the viewstate values?

I've already tried the Me.Viewstate.Clear() method, but it seems doesn't work.

What I am trying to do is to develop a webpage, where the admins could add new users into the database.
I've coded the page so that whenever the page is postback, the validation and add user process begins.
If the data entered wasn't valid, it would return to same page with a warning message and all data in it's place (this is correct).
But now if all the data was correct and user added into database, I wanted to also return to same page with an update message, but all viewstate values cleared out, meaning that all values in controls (textbox, listbox etc) are empty.

Can someone help me please?
0
Comment
Question by:guest321
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 4
12 Comments
 

Author Comment

by:guest321
ID: 12155464
Hmm I just figured another simple way (but don't know if it's efficient). By setting all textboxes' text value to empty string and listboxes' selectedindex to 0 before postback...

But I'm still looking forward to methods like Viewstate.Clear().
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12155593
You can always clear an element like this:

ViewState("YourFieldName") = ""



I haven't tried this, but it may work:

for each objItem in ViewState()
   ViewState(objItem) = ""
next

FtB
 
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12156315
Question:
Where exactly are you currently calling the Me.Viewstate.Clear( ) method?  (In the page's Init or Load event handler, or...?)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:guest321
ID: 12159517
fritz_the_blank:
Thanks very much! I'll try that out when I have access to my computer later today. :)

Thogek:
My code looked something like the following:

Private Sub Page_Load(...)
   If Page.IsPostBack Then
      If Update() Then
         Me.Viewstate.Clear()
         ' Issue updated message
      Else
         ' Issue error message
      End If
   End If
End Sub
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12159671
BTW--I saw a lot of discussions out there regarding ways to clear view state, and I haven't come accross anything like .Clear() yet. I suspect that is not a valid option. I was wondering if Contents.Remove() might work, but I haven't seen that referenced either.

Of course, you could always set enableViewState to false...

FtB
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12163788
Fritz,
The Control's (and thus the Page's) ViewState property object is actually an instance of the StateBag class (http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebUIControlClassViewStateTopic.asp), and the StateBag class does have a Clear method (http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuistatebagclasscleartopic.asp) which appears that it should do what guest321 expects.
0
 
LVL 15

Accepted Solution

by:
Thogek earned 250 total points
ID: 12163882
guest321,
The ViewState is actually loaded and used to restore control values before the Load event is fired, so clearing your loading page's ViewState during the Load event won't have much effect on anything, as the ViewState has already been used by then.

See "Control Execution Lifecycle" at http://msdn.microsoft.com/library/en-us/cpguide/html/cpconcontrolexecutionlifecycle.asp.

You could try moving your code into the Init event handler method, but I suspect that, since that event fires before ViewState has been read, there'd probably be nothing there yet to Clear.

You might be able to get what you're trying to do by, instead of doing so within Page_Load, override the Page's LoadViewState method and place your code in there.  Something like:

    Protected Overrides Sub LoadViewState(savedState As Object)
        MyBase.LoadViewState(savedState)
        If Page.IsPostBack Then
            If Update() Then
                Me.Viewstate.Clear()
                ' Issue updated message
            Else
                ' Issue error message
            End If
        End If
    End Sub

Disclaimer: I haven't tested this code, and I'm not familiar with what your Update method might be trying to do.  :-O
0
 

Author Comment

by:guest321
ID: 12184817
fritz_the_blank:

Unfortunately by disabling the EnableViewState property, it still worked as if the EnableViewState were set to true. I was wondering if these properties doesn't work, they must exist for some reason, perhaps I did it incorrectly..? :-/

Thogek:

Yeah, you were right about the Page_Init() event. The viewstate didn't cleared out. The only way that I could think of now is to clear all values manually. I understand this might not be the best way to do it, and it requires a bit more of coding, but I'll use it until I can find a better solution. :-)

Thank you all for the great help! I really do appreciate it. :-)
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12184904
If you are clearing all of the values manually, isn't that what I showed you in my first post?

FtB
0
 
LVL 15

Expert Comment

by:Thogek
ID: 12185186
Did you try overloading the LoadViewState method, as suggested above?
0
 

Author Comment

by:guest321
ID: 12208691
fritz_the_blank:

I've tried to clear out the viewstate elements like the way you showed me, but after testing the code I realized the value always return empty string even before the execution of assigning statement, and the form values still preserved, so that's why I manually set the text values to empty string instead of viewstate elements.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 12208709
Thanks for letting me know--I am just starting to learn ASP.Net now, so I am interested in how people end up solving problems and etc.

FtB
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Select case on click 3 26
Html split(text) 2 27
Session on Html 8 38
Validating number not work with decimal 4 23
I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question