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 N开发者_开发知识库ot 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.
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");
}
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")) ...
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
Dim testValue As String = CStr(ViewState("test"))
If testValue.IndexOf("tom") = -1 Then
'add it....
testValue += testValue & ", tom" 'will add to end of string
ViewState("test") = testValue
End If
精彩评论