Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Clearing ASP.NET Viewstate

Posted on 2004-09-26
12
Medium Priority
?
9,186 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 1000 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

Industry Leaders: 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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

721 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