473,382 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ViewState.Count = 0

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

May 25 '06 #1
6 4431
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.

May 25 '06 #2
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

May 25 '06 #3
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"pa*********@gmail.com" wrote:
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

May 25 '06 #4
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

May 25 '06 #5
No
<pa*********@gmail.com> wrote in message
news:11*********************@38g2000cwa.googlegrou ps.com...
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

May 25 '06 #6

<pa*********@gmail.com> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
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>
May 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: theo | last post by:
Hi... I wish to extract the text content of an Xml file and assign it to DropDownList controls at runtime.I can get the Xml file text content into the DropDownList controls (Ex...if 5 Xml text...
6
by: wASP | last post by:
Hi again, I'm having a problem accessing the ViewState object. I'm using the following two functions - as copied from the MS docs - and state.count is zero in the first - and the while loop in...
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
3
by: Steve Drake | last post by:
All, I have a CONTROL that contains 1 control (Control ONE), the 1 control that it can contain 1 or 2 control (Control A and B). Control A, raises and event and Control ONE receives this event...
8
by: Invalidlastname | last post by:
Hi, We are developing an asp.net application, and we dynamically created certain literal controls to represent some read-only text for certain editable controls. However, recently we found an issue...
1
by: Ralph Soons | last post by:
Hi all, I am trying to save the viewstate in a session instead of storing it in a hidden of the webpage which is default. This because of performance reasons. When I use line 2 in combination...
10
by: Robert | last post by:
I have an app that was originally 1.1, now migrated to 2.0 and have run into some sporadic viewstate errors...usually saying the viewstate is invalid, eventvalidation failed or mac error. My web...
6
by: Peter Zolja | last post by:
Hi, I'm building a webcontrol that contains a dynamic list of other controls. My problem is that when I add or remove an item the synchronization between the ViewState and the Controls...
1
by: Mark Olbert | last post by:
I have a "master" composite control which, in turn, holds an instance of a "detail" composite control (the "master" control will ultimately contain multiple instances of the "detail" control, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.