Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Can't get viewstate to work

Reply
Thread Tools

Can't get viewstate to work

 
 
multiformity@gmail.com
Guest
Posts: n/a
 
      05-25-2006
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">
<asplaceHolder 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 />";
}
}

 
Reply With Quote
 
 
 
 
Will Asrari
Guest
Posts: n/a
 
      05-25-2006
i = (int)ViewState["index"];

try...

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

 
Reply With Quote
 
 
 
 
multiformity@gmail.com
Guest
Posts: n/a
 
      05-25-2006
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());


 
Reply With Quote
 
vMike
Guest
Posts: n/a
 
      05-25-2006

<> wrote in message
news: 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 />";
> }
> }
>




 
Reply With Quote
 
Jeff Dillon
Guest
Posts: n/a
 
      05-25-2006
And it looks like i is always an unitialized variable

<> wrote in message
news: 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());

>



 
Reply With Quote
 
multiformity@gmail.com
Guest
Posts: n/a
 
      05-25-2006
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

 
Reply With Quote
 
vMike
Guest
Posts: n/a
 
      05-26-2006

<> wrote in message
news: 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">
<asplaceholder id="plc1" runat="server"/>
</form>
</body>


 
Reply With Quote
 
vMike
Guest
Posts: n/a
 
      05-26-2006
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">
<asplaceholder id="plc1" runat="server"/>
</form>
</body>


 
Reply With Quote
 
multiformity@gmail.com
Guest
Posts: n/a
 
      05-30-2006
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

 
Reply With Quote
 
multiformity@gmail.com
Guest
Posts: n/a
 
      05-30-2006
In case anyone is interested, I documented my code and findings at the
following location:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with ViewState: "function 'ViewState.get_Item' evaluated and returned null" Steph ASP .Net 2 05-11-2011 02:35 PM
Loading usercontrols, viewstate problem, slighly different from all others "viewstate uc problems" please help... ujjc001 ASP .Net 0 07-27-2005 01:52 PM
Viewstate errors... how do I get viewstate working? mark ASP .Net Building Controls 0 02-20-2004 02:17 PM
Unable to get viewstate to work with dynamic controls... Tom Wisnowski ASP .Net 4 12-06-2003 06:05 AM
Corrupted ViewState (Yes, another issue concerning viewstate) Ben Rush ASP .Net 2 12-05-2003 04:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57