Solved

Clearing ASP.NET Viewstate

Posted on 2004-09-26
12
8,581 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
  • 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now