Check for a value in ViewState
Solution 1
Assuming you put the values in as a single string:
ViewState("test") = "bob, tom, jack"
If Not ViewState("test").ToString().Contains("tom") Then
ViewState("test") = ViewState("test").ToString() And ", tom"
End If
The above would work if you're using a single string to store the names. You may want to consider a collection object such as List.
You could then have:
Dim names As New List(Of String)() From { _
"tom", _
"jack", _
"harry" _
}
ViewState("test") = names
Dim viewstateNames As List(Of String) = TryCast(ViewState("test"), List(Of String))
If viewstateNames IsNot Nothing AndAlso Not viewstateNames.Contains("tom") Then
viewstateNames.Add("tom")
End If
On a side note, I hate VB.NET. You should consider C#. So much simpler to follow:
List<string> names = new List<string> { "tom", "jack", "harry" };
ViewState["test"] = names;
List<string> viewstateNames = ViewState["test"] as List<string>;
if (viewstateNames != null && !viewstateNames.Contains("tom"))
{
viewstateNames.Add("tom");
}
Solution 2
You can check known keys of ViewState by just interrogating the key; the following will not throw an exception if "test" hasn't been set in ViewState:
if(ViewState["test"] == null) ...
ViewState, being a StateBag, is also an IEnumerable, meaning you can iterate through its contents using foreach, or use Linq to search by value:
ViewState.Where(si=>si.IsDirty); //gets all ViewState members that changed since the last postback
Lastly, ViewState exposes collections of its Keys and Values as ICollections. These can also be iterated as an ICollection is an IEnumerable, meaning you can use Linq to check for the existence of a key:
if(ViewState.Keys.Any(k=>k == "test")) ...
Solution 3
Typically, you would first test that the ViewState item exists.
if (ViewState["Foo"] != null)
{
// maybe I can do something here
}
If it does, the next step is to retrieve the object stored with the specified key. Since it is stored as an object, we would need to cast to what the type really is in our code.
if (ViewState["Foo"] != null)
{
List<string> values = (List<string>)ViewState["Foo"];
// do something with values
}
At which point, performing our test of a particular value's existence inside our list to be a matter of normal code.
if (ViewState["Foo"] != null)
{
List<string> values = (List<string>)ViewState["Foo"];
if (!values.Contains("tom"))
{
values.Add("tom");
}
}
In VB, that would probably be something like
If Not (ViewState("Foo") Is Nothing) Then
List(of String) values = CType(ViewState("Foo"), List(of String))
If Not (values.Contains("tom")) Then
values.Add("tom")
End If
End If
![StealthRT](https://i.stack.imgur.com/JMaKB.jpg?s=256&g=1)
StealthRT
Updated on September 18, 2020Comments
-
StealthRT almost 4 years
Hey all, i am new at the ViewState and i am looking to see what values are stored within it. I know how to loop through an array to check for a value but i do not know how to go about doing that for a ViewState("test") property.
Let's say i have added "bob, tom, Jim" to a ViewState called ViewState("test"). I would like to be able to see if "tom" was in the ViewState("test"). If not then add it but if it exists already then skip it.
Thanks!
David
THE CODE
Public strIDArray() As String Public vsSaver As String Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then ViewState("vsSaver") = "0000770000" Else If (Not ViewState("vsSaver") Is Nothing) Then strIDArray(strIDArray.Length) = CType(ViewState("vsSaver"), String) End If End If end sub Private Sub gvData_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvData.RowCommand Dim idIndexNumber As Integer = Array.IndexOf(strIDArray, strID) If Not ViewState("strIDArray").ToString().Contains(strID) Then strIDArray(idIndexNumber + 1) = strID ViewState("strIDArray") = CLng(ViewState("strIDArray").ToString()) And CLng(strID) End If End Sub
I'm so confused.. ha.
-
KP. almost 14 yearsCan you post some code? How did you add the names? As a string array? A single string? We can't really answer this question without knowing.
-
StealthRT almost 14 yearsI've added the code above, KP.
-
-
KP. almost 14 yearsI think the OP was interested in checking a value within the viewstate object with key
test
, not just that the key/item exists. -
StealthRT almost 14 yearsI've added my code in my OP above but i am a little confused about how to save it (and where) and how to retrieve it in a loop type of thing..
-
StealthRT almost 14 yearsI've added my code in my OP above but i am a little confused about how to save it (and where) and how to retrieve it in a loop type of thing..
-
StealthRT almost 14 yearsI've added my code in my OP above but i am a little confused about how to save it (and where) and how to retrieve it in a loop type of thing..