[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9476
  • Last Modified:

Clearing ASP.NET Viewstate

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
guest321
Asked:
guest321
  • 4
  • 4
  • 4
1 Solution
 
guest321Author Commented:
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
 
fritz_the_blankCommented:
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
 
ThogekCommented:
Question:
Where exactly are you currently calling the Me.Viewstate.Clear( ) method?  (In the page's Init or Load event handler, or...?)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
guest321Author Commented:
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
 
fritz_the_blankCommented:
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
 
ThogekCommented:
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
 
ThogekCommented:
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
 
guest321Author Commented:
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
 
fritz_the_blankCommented:
If you are clearing all of the values manually, isn't that what I showed you in my first post?

FtB
0
 
ThogekCommented:
Did you try overloading the LoadViewState method, as suggested above?
0
 
guest321Author Commented:
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
 
fritz_the_blankCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now