ViewState.Count = 0

  • Thread starter Thread starter paul.hester
  • Start date Start date
P

paul.hester

Hi all,

Does anyone know why the ViewState would be empty?

When I'm receiving a postback, I can access a posted value using
controlName.Value but not ViewState["controlName"].

I have EnableViewState set to true in my master page and content page.

Thanks,

Paul
 
As far as I know this isn't how viewstate works. If you set a variable
ViewState["Foo"] = "FooValue", then it will be available on the next
post. The ViewState is represented as a triplet data structure, not as
an array of controls. The control loads its own viewstate between the
init and load events.
 
I thought if view state was enabled, the values of the controls would
be in the ViewState collection. Is this definitely not the case?

I'm trying to access the view state in Page_Load, if that helps.

Thanks,

Paul
 
Paul,
No. Viewstate represents the state of the page when it was last processed on
the server. ViewState is used to track and restore the state values of
controls that would otherwise be lost, either because those values do not
post with the form or because they are not in the page html.

A control defined solely in your page html with no changes made in the code
will have no ViewState. ViewState only holds the values of properties that
are dynamically changed somehow, usually in code, data-binding, or user
interactions, so that they can be restored on each request.

ViewSate does not hold "controls", it only hold values and the ID's of the
controls they belong to.


Hope that clarifies.
Peter
 
Thanks Peter,

I'm aware that ViewState doesn't hold controls, but I expected to be
able access control values in the Page_Load with ViewState enabled. If
a control has ViewState enabled shouldn't its value be accessible in
the ViewState collection? e.g. for a text input:
ViewState["controlName"].ToString(), or something to that effect.

Thanks,

Paul
 
Hi all,

Does anyone know why the ViewState would be empty?

When I'm receiving a postback, I can access a posted value using
controlName.Value but not ViewState["controlName"].

I have EnableViewState set to true in my master page and content page.

Thanks,

Paul
Seems to be a lot of confusion about viewstate. Maybe this will help.
Mike

<%@ Page %>
<%@ import Namespace="System.Net" %>
<html>
<script language="vb" runat="server">

Protected Overrides Function SaveViewState() As Object
Return MyBase.SaveViewState()
End Function

Protected Overrides Sub LoadViewState(savedState As Object)
If Not (savedState Is Nothing) Then
MyBase.LoadViewState(savedState)
Response.write("<br><br>Programmed ViewState items at LoadViewState: "
& GetMruList())

End If
End Sub

Sub Page_Load(sender as object, e as eventargs)
if not ispostback then
viewstate("New") = "New Page that is not Postback"
response.write("<br><br>Programmed ViewState item at New: " &
GetMruList())
else
dim strViewState as string = request.form("__Viewstate").tostring()
dim vdata as byte() = Convert.FromBase64String(strViewState)
dim strDecode as string = Encoding.ASCII.GetString(vdata)
response.write("<br><br>This is the .net coded viewstate for the page
" & strViewstate)
Response.write("<br><br>This is the .net decoded viewstate for the
page ")
response.write(Server.HtmlEncode(strDecode))
end if
getdatactl()
if not ViewState("Drop") is nothing then Response.Write("<br><br>Direct
call to Drop Viewstate Item. Value is: " & ViewState("Drop"))
end Sub

Function GetMruList() As String
Dim state As StateBag = ViewState
If state.Count > 0 Then
Dim upperBound As Integer = state.Count
Dim keys(upperBound) As String
Dim values(upperBound) As StateItem
state.Keys.CopyTo(keys, 0)
state.Values.CopyTo(values, 0)
Dim options As New StringBuilder()
Dim i As Integer
For i = 0 To upperBound - 1
options.append(keys(i) & " - " & values(i).value & ";")
Next i
Return options.ToString()
End If
Return ""
End Function 'GetMruList

sub GetDataCtl()
dim i as int32
dim strI as string
dim j as int32 = 5
dim drpCtl as new DropDownList
drpCtl.AutoPostBack="True"

for i = 0 to j
strI = i.tostring()
dim lst as new listitem("hello" + strI, stri)
drpCtl.items.add(lst)

dim imgCtl as new imagebutton
imgctl.imageurl = "someimage.jpg"
imgctl.id = "img" + strI
imgctl.commandargument = "Hello" & strI
addhandler imgctl.click, addressof ImageButton1_Click
plc1.controls.add(imgctl)

next i
addhandler drpCtl.SelectedIndexChanged, addressof DropList_Change
plc1.controls.add(drpCtl)

End sub

sub ImageButton1_Click(sender as object, e as ImageClickEventArgs)
viewstate("Image") = "Image whatever"
response.write("<br><br>Programmed ViewState items at Image Click: " &
GetMruList())
End sub

Sub DropList_Change(sender as object, e as eventargs)
viewstate("Drop") = "Drop whatever"
response.write("<br><br>Programmed ViewState items at DropListChange: " &
GetMruList())
End Sub

</script>
<body>
<form runat="server">
<h2>Click blank images or change dropdown box to see viewstate
change.</h2><br>
<asp:placeholder id="plc1" runat="server"/>
</form>
</body>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top