473,398 Members | 2,525 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,398 software developers and data experts.

Can't get viewstate to work

The following page will not load the view state from the statebag
without throwing an exception. After we get this working, I will be
working on viewstate for some custom controls, but one thing at a time.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/// <summary>
/// Simple test of view state, we are trying to persist the value for i
among multiple postbacks.
/// </summary>
/*
Use the following ASPX page to test this class:
**********************************************
<%@ Page language="c#" AutoEventWireup="false" Inherits="TestPostback"
CodeFile="TestPostback.aspx.cs" EnableViewState="true" %>
<HTML>
<body>
<form runat="server" id="GridForm">
<asp:PlaceHolder ID="_Content" Runat="server" />
<br />
<asp:Literal ID=_Literal runat=server />
</form>
</HTML>
***********************************************
*/
public partial class TestPostback : Page {

private int i;

override protected void OnInit(EventArgs e) {
this.EnableViewState = true;
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
_Content.EnableViewState = true;
}

protected override void LoadViewState(object savedState) {
try {
//This line throws an exception,
//and the viewstate collection is empty
i = (int)ViewState["index"];
_Literal.Text = (string)ViewState["text"];
base.LoadViewState(savedState);
} catch { }
}

protected override object SaveViewState() {
ViewState["index"] = i;
ViewState["text"] = _Literal.Text;
return base.SaveViewState();
}

protected void Page_Load(object sender, System.EventArgs e) {
++i;
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Text = "Click";
_Content.Controls.Add(button);
}

void button_Click(object sender, EventArgs e){
//We will note here that the _Literal controls state
//has persisted, but the index has not incremented.
_Literal.Text += i.ToString() + "<br />";
}
}

May 25 '06 #1
9 1484
i = (int)ViewState["index"];

try...

i = Int.Parse(ViewState["index"].ToString());

May 25 '06 #2
After looking into it further I found that the ViewState colletion's
count is 0 every time that the LoadViewState method is called.

I also verified the order that the methods are firing

(open page)
Page Load, SaveViewState

(button click)
LoadViewState, Page Load, button Click, SaveViewState

(button click)
LoadViewState, Page Load, button Click, SaveViewState

(repeat)

I commented all of this and have a modified program that should just
spit out the current location of the progress in the Literals text
property, but it doesn't seem to be working, you can see it all at:

http://rafb.net/paste/results/RdHSXo56.html

I know it's a long program now, but a lot of that is just commenting
and stuff
AB

Will Asrari wrote:
i = (int)ViewState["index"];

try...

i = Int.Parse(ViewState["index"].ToString());


May 25 '06 #3

<mu**********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
The following page will not load the view state from the statebag
without throwing an exception. After we get this working, I will be
working on viewstate for some custom controls, but one thing at a time.
*/
public partial class TestPostback : Page {

private int i;

override protected void OnInit(EventArgs e) {
this.EnableViewState = true;
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
_Content.EnableViewState = true;
}
I think it is b/c your are looking for something in viewstate before your
have loaded it.
The LoadViewState is load from the page when it is posted back as I
understand it.
Also you should check the saveState before loading in as follows.

protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
// Load State from the array of objects that was saved at ;
// SavedViewState.
object[] myState = (object[])savedState;
if (myState[0] != null)
base.LoadViewState(myState[0]);
if (myState[1] != null)
UserText = (string)myState[1];
if (myState[2] != null)
PasswordText = (string)myState[2];
}
}
The saveview state is what is saved to the page in the response.
protected override void LoadViewState(object savedState) {
try {
//This line throws an exception,
//and the viewstate collection is empty
i = (int)ViewState["index"];
_Literal.Text = (string)ViewState["text"];
base.LoadViewState(savedState);
} catch { }
}

protected override object SaveViewState() {
ViewState["index"] = i;
ViewState["text"] = _Literal.Text;
return base.SaveViewState();
}

protected void Page_Load(object sender, System.EventArgs e) {
++i;
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Text = "Click";
_Content.Controls.Add(button);
}

void button_Click(object sender, EventArgs e){
//We will note here that the _Literal controls state
//has persisted, but the index has not incremented.
_Literal.Text += i.ToString() + "<br />";
}
}


May 25 '06 #4
And it looks like i is always an unitialized variable

<mu**********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
After looking into it further I found that the ViewState colletion's
count is 0 every time that the LoadViewState method is called.

I also verified the order that the methods are firing

(open page)
Page Load, SaveViewState

(button click)
LoadViewState, Page Load, button Click, SaveViewState

(button click)
LoadViewState, Page Load, button Click, SaveViewState

(repeat)

I commented all of this and have a modified program that should just
spit out the current location of the progress in the Literals text
property, but it doesn't seem to be working, you can see it all at:

http://rafb.net/paste/results/RdHSXo56.html

I know it's a long program now, but a lot of that is just commenting
and stuff
AB

Will Asrari wrote:
i = (int)ViewState["index"];

try...

i = Int.Parse(ViewState["index"].ToString());

May 25 '06 #5
GRR

that was dumb, somethin is still wrong, but I got it working like so

override protected void OnInit(EventArgs e) {
this.EnableViewState = true;
//had to explicitly remove the enable view state for the label
_Literal.EnableViewState = false;
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
_Content.EnableViewState = true;
}

protected override void LoadViewState(object savedState) {
//For some stupid reason I can access the saved state arraylist,
// but not the viewstate's collection
if (savedState != null) {
ArrayList list = (ArrayList)savedState;
try {
//I just happen to know that they are 1 and 3
//But why bother even haveing the viewstate then? I can
//write a specialized collection that does what the
//viewstate statebag does....
i = int.Parse(list[1].ToString());
_Literal.Text += list[3].ToString();
} catch (Exception ex) {
string junk = ex.Message;
_Literal.Text += ex.Message + "<br />";
}
}
base.LoadViewState(savedState);
}

complete listing here

http://rafb.net/paste/results/cGTR9O27.html

I am still interested in knowing why the viewstate doesn't work in the
load, is that supposed to be my job somehow?

AB

May 25 '06 #6

<mu**********@gmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.com...
The following page will not load the view state from the statebag
without throwing an exception. After we get this working, I will be
working on viewstate for some custom controls, but one thing at a time.

I think it is because you can only use x = Viewstate("youritem") for things
that you put into view state. If you want to look at the rest of the view
state you have to do something like the following. (this is vb but you can
easily rewrite in c.

<%@ 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>Viewstate as LoadViewState: " & Viewstate("Test"))
End If
End Sub

Sub Page_Load(sender as object, e as eventargs)
if not ispostback then
viewstate("Test") = "Not Postback"
response.write("<br>Viewstate 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>This is the coded viewstate " & strViewstate)
Response.write("<br>This is the decoded viewstate")
response.write(Server.HtmlEncode(strDecode))
end if
getdatactl()
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)
'options.AppendFormat("<option {0} value={1}>{2}",IIf(selectedValue
= keys(i), "selected", ""), 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 = 10
for i = 0 to j
strI = i.tostring()
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
End sub

sub ImageButton1_Click(sender as object, e as ImageClickEventArgs)
viewstate("Test") = "Postback"
response.write("<br>ViewState as Click: " & GetMruList())
End sub
</script>
<body>
<form runat="server">
<asp:placeholder id="plc1" runat="server"/>
</form>
</body>
May 26 '06 #7
This may even be a bit better example. Watch what happens when you change
the dropdownlist.
<%@ Page Trace=false %>
<%@ 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>Viewstate as LoadViewState: " & Viewstate("Test"))
End If
End Sub

Sub Page_Load(sender as object, e as eventargs)
if not ispostback then
viewstate("Test") = "Not Postback"
response.write("<br>Viewstate 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>This is the coded viewstate " & strViewstate)
Response.write("<br>This is the decoded viewstate")
response.write(Server.HtmlEncode(strDecode))
end if
getdatactl()
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)
'options.AppendFormat("<option {0} value={1}>{2}",IIf(selectedValue
= keys(i), "selected", ""), 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 = 10
dim drpCtl as new DropDownList
drpCtl.AutoPostBack="True"
for i = 0 to j
strI = i.tostring()
dim imgCtl as new imagebutton

dim lst as new listitem("hello" + strI, stri)
drpCtl.items.add(lst)
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("Test") = "Postback"
response.write("<br>ViewState as Click: " & GetMruList())
End sub

Sub DropList_Change(sender as object, e as eventargs)
viewstate("Test") = "Postback"
response.write("<br>ViewState as DropListChange: " & GetMruList())
End Sub
</script>
<body>
<form runat="server">
<asp:placeholder id="plc1" runat="server"/>
</form>
</body>
May 26 '06 #8
I figured it out about 10 minutes after my last post, just didn't post
my new code.

Basically in the loadview state I had to call

base.LoadViewState(sender) before I did anything...

Now onto the component viewstate, which is going to be a little more
complicated I am sure, so keep an eye out for that one guys!
AB

May 30 '06 #9
In case anyone is interested, I documented my code and findings at the
following location:

http://www.ats-engineers.com/lifecycle.htm

May 30 '06 #10

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

Similar topics

5
by: Christian | last post by:
Hi, Viewstate of WebUserControl does NOT work although the 'EnableViewstate'-property of the usercontrol is set to true Description : Using ASP.Net : I have a created a WebPage with 2...
2
by: Christian | last post by:
Hi, Viewstate of WebUserControl does NOT work although the 'EnableViewstate'-property of the usercontrol is set to true Description : Using ASP.Net : I have a created a WebPage with 2...
0
by: Moheb Missaghi | last post by:
Hi: I am trying to use the Transfer statement in an .aspx file to redirect and send Form and QueryString collections to a different page. A good example where this is needed is a checkout page...
3
by: Paul | last post by:
Hi I am setting a boolean value to true on a page and writing the results to viewstate Just above the web form designer generated code I have Dim bps_found As Boolean 'visible to this module in...
3
by: Jim Corey | last post by:
I'm trying to save some ArrayLists to viewstate and then use them as DataSources for dropdownboxes. The code looks like this: 'DptList is a arraylist variable local to this procedure ...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
6
by: mosscliffe | last post by:
I am testing for how/when a page is posted back and I decided to use a ViewState variable in PageLoad to set up a counter, but it appears, the ViewState is cleared on each PageLoad. So then I used...
0
by: Walter | last post by:
Hi, can someone please help me with my custom control viewstate problem....I haven't slept for hours trying to get this fixed. I am making two custom controls which will be included on a single...
2
by: dotComPaJi | last post by:
Hello There, I have a simple question. In one of my program if I use ViewState then program does not work as expected(nothing is displayed in GridView but If I use Session State then it works...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.