Your code is using sessions for storage and retrieval of data, but you
are talking about viewstate. Session data and ViewState data are not
the same thing.
Also, you still seem to be saying that SVS is firing twice because of
an assumption, not because of a fact. Take all your code out of SVS
and just place a dummy line of code (create a variable and assign it a
value). Set a breakpoint on this line and execute your code (with
debugging) and step (line by line) through the code. I really think
under this scenario, you will see SVS fires only once per page request.
Lastly, even if you wish to add to ViewState, I still wouldn't do it
from within the SVS or LVS events. Do it from Page_Load and use
IsPostBack if needed.
I got this from:
http://jarednevans.typepad.com/technoblog/2005/01/jareds_techno_b.html
It does seem to work fine for Pages that have already been posted back,
but doesn't work for initial pages that are being refreshed. This is
because the LoadViewState doesn't fire. I was looking at using a
session variable (similar to Dino Espositos solution that is mentioned
in the above article) is some way to handle this.
What is your reason for not doing it this way? Is it that you don't
like the base Page Class setup? I want to set this up so that all my
using it and this seemed like a pretty good place to do this (except for
the LoadViewState problem - which I am looking at).
I belive you are not getting the results you want because of the code
you wrote and not because the SVS event is working incorrectly.
You were right. It wasn't what I wrote but that I had the trace set to
true. This causes the SVS to fire a second time (I assume the event that
fires between the normal page events is from this).
This is why it was confusing. It would work fine then all of a sudden
it wouldn't.
Using the following:
*************************************
using System;
using System.Web;
using System.Web.SessionState;
namespace MyFunctions
{
public class Page : System.Web.UI.Page
{
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
}
protected override object SaveViewState()
{
return base.SaveViewState();
}
}
}
**********************************************
Works fine if trace="false", but fires a 2nd time if trace="true".
Do you know why it does that?
Thanks,
Tom
First, I would try setting breakpoints and stepping through your code
line by line to verify your assertion that SVS is firing twice during
one page request, rather than assume this is the case by looking at
the trace. The trace is not necessarially an event by event listing
of what happend during the page request. It's more of a stage by
stage description.
I only did that to check if the code was working correctly.
Second, I wouldn't be adding any code to the LVS or the SVS event
handlers unless that code has to do with the loading or saving of
viewstate. Writing out to a text file doesn't need to be done here.
Use Page_Load instead.
The actual code was loading data into the viewstate.
I was writing into my textfile only to see what was happening.
What I found was that the SaveViewState was being fired twice because
on the first Session(ISREFRESH) was equal to nothing and on the second
time it was fired it would be "false"
At start of SaveViewState - _refreshState = False
Session(ISREFRESH) = isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRESH)
= False isRefresh = False
At start of SaveViewState - _refreshState = False
Session(ISREFRESH) = False isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRESH)
= False isRefresh = False
********************************************************************************************
protected override object SaveViewState()
{
bool _isrefresh = false;
string _tname = "__Ticket";
FileStream fs = new
FileStream("c:\\inetpub\\wwwroot\\staffingworkshop\\uploads\\tfsRefresh.txt",
FileMode.Append, FileAccess.Write);
StreamWriter s = new StreamWriter(fs);
s.BaseStream.Seek(0,SeekOrigin.End);
s.WriteLine(" ");
s.WriteLine("C# Version");
s.WriteLine("At start of SaveViewState - _refreshState = " +
_refreshState + " Session(ISREFRESH) = " + Session["__ISREFRESH"] +
" isRefresh = " + _isRefresh);
Session["__ISREFRESH"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewState();
allStates[1] = !_refreshState;
s.WriteLine("At End of SaveViewState - _refreshState = " +
_refreshState + " Session(ISREFRESH) = " + Session["__ISREFRESH"] + "
isRefresh = " + _isRefresh);
s.Close();
return allStates;
}
}
**********************************************************************************
The thing is this didn't use to be the case. I looked at my old text
file and it was only firing once. Something is causing it to fire
twice now. I started stripping out code to see if something I was
doing was causing the problem, but when I got it down to the bare
minimum, it was still doing it.
This was driving me crazy as there didn't seem to be any rhyme or
reason for this happening. As you can see from below there is nothing
there. So what could be causing it. I tried restarting my machine to
see if there was a problem there, but that didn't do it.
Tom
I thought I understood how the SaveViewState is working and was
trying to use this (as per some code I found) to detect refreshes.
It seemed to be working but I found that the SaveViewState was
executing twice on load of page and can't figure out why.
I was writing out to a text file in the SaveViewState event and it
was writing it twice everytime a page was loaded (whether on an
initial load of the page or Postback).
I took out the writes to the text file and added the "trace.warn" to
see where it was happening.
The thing I noticed is that the first SaveViewState is executing
outside all the Events.
It is executing after the End of the PreRender and before the Start
of the normal SaveViewState.
aspx.page Begin Init
aspx.page End Init 0.000072 0.000072
aspx.page Begin PreRender 0.000111 0.000039
aspx.page End PreRender 0.000160 0.000049
Inside refresh.cs at SaveViewState 0.000884 0.000725
aspx.page Begin SaveViewState 0.001542 0.000657
Inside refresh.cs at SaveViewState 0.001885 0.000344
aspx.page End SaveViewState 0.001929 0.000044
aspx.page Begin Render 0.001956 0.000027
aspx.page End Render 0.111191 0.109235
Just to make sure my code was working correctly, I rewrote the class
to do nothing except what is done normally.
namespace MyFunctions
{
public class Page : System.Web.UI.Page
{
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
}
protected override object SaveViewState()
{
Trace.Warn("Inside refresh.cs at SaveViewState");
return base.SaveViewState();
}
}
}
I am still getting the same results.
It calls the SaveViewState twice. This is not what the
documentations say (at least not that I can find).
Am I missing something?
Thanks,
Tom