473,695 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SaveViewState executing twice

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.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}

protected override object SaveViewState()
{
Trace.Warn("Ins ide refresh.cs at SaveViewState") ;
return base.SaveViewSt ate();
}
}
}

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
Aug 25 '06 #1
7 2807
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.

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.
"tshad" <ts**********@f tsolutions.comw rote in message
news:et******** ******@TK2MSFTN GP04.phx.gbl...
>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.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}

protected override object SaveViewState()
{
Trace.Warn("Ins ide refresh.cs at SaveViewState") ;
return base.SaveViewSt ate();
}
}
}

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

Aug 25 '06 #2

"Scott M." <s-***@nospam.nosp amwrote in message
news:eO******** ******@TK2MSFTN GP02.phx.gbl...
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(ISREFRE SH) was equal to nothing and on the second time it was
fired it would be "false"

At start of SaveViewState - _refreshState = False Session(ISREFRE SH) =
isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False

At start of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False
*************** *************** *************** *************** *************** *************** **
protected override object SaveViewState()
{
bool _isrefresh = false;
string _tname = "__Ticket";

FileStream fs = new
FileStream("c:\ \inetpub\\wwwro ot\\staffingwor kshop\\uploads\ \tfsRefresh.txt ",
FileMode.Append , FileAccess.Writ e);
StreamWriter s = new StreamWriter(fs );
s.BaseStream.Se ek(0,SeekOrigin .End);
s.WriteLine(" ");
s.WriteLine("C# Version");
s.WriteLine("At start of SaveViewState - _refreshState = " + _refreshState +
" Session(ISREFRE SH) = " + Session["__ISREFRES H"] + " isRefresh = " +
_isRefresh);
Session["__ISREFRES H"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewSt ate();
allStates[1] = !_refreshState;
s.WriteLine("At End of SaveViewState - _refreshState = " + _refreshState + "
Session(ISREFRE SH) = " + Session["__ISREFRES H"] + " 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
>

"tshad" <ts**********@f tsolutions.comw rote in message
news:et******** ******@TK2MSFTN GP04.phx.gbl...
>>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.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}

protected override object SaveViewState()
{
Trace.Warn("In side refresh.cs at SaveViewState") ;
return base.SaveViewSt ate();
}
}
}

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


Aug 25 '06 #3
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 belive you are not getting the results you want because of the code you
wrote and not because the SVS event is working incorrectly.

"tshad" <ts**********@f tsolutions.comw rote in message
news:Ow******** ******@TK2MSFTN GP04.phx.gbl...
>
"Scott M." <s-***@nospam.nosp amwrote in message
news:eO******** ******@TK2MSFTN GP02.phx.gbl...
>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(ISREFRE SH) was equal to nothing and on the second time
it was fired it would be "false"

At start of SaveViewState - _refreshState = False Session(ISREFRE SH) =
isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False

At start of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False
*************** *************** *************** *************** *************** *************** **
protected override object SaveViewState()
{
bool _isrefresh = false;
string _tname = "__Ticket";

FileStream fs = new
FileStream("c:\ \inetpub\\wwwro ot\\staffingwor kshop\\uploads\ \tfsRefresh.txt ",
FileMode.Append , FileAccess.Writ e);
StreamWriter s = new StreamWriter(fs );
s.BaseStream.Se ek(0,SeekOrigin .End);
s.WriteLine(" ");
s.WriteLine("C# Version");
s.WriteLine("At start of SaveViewState - _refreshState = " + _refreshState
+ " Session(ISREFRE SH) = " + Session["__ISREFRES H"] + " isRefresh = "
+ _isRefresh);
Session["__ISREFRES H"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewSt ate();
allStates[1] = !_refreshState;
s.WriteLine("At End of SaveViewState - _refreshState = " + _refreshState +
" Session(ISREFRE SH) = " + Session["__ISREFRES H"] + " 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
>>

"tshad" <ts**********@f tsolutions.comw rote in message
news:et******* *******@TK2MSFT NGP04.phx.gbl.. .
>>>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.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}

protected override object SaveViewState()
{
Trace.Warn("I nside refresh.cs at SaveViewState") ;
return base.SaveViewSt ate();
}
}
}

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



Aug 25 '06 #4
"Scott M." <s-***@nospam.nosp amwrote in message
news:Ol******** ******@TK2MSFTN GP06.phx.gbl...
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/techn..._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.Sess ionState;

namespace MyFunctions
{
public class Page : System.Web.UI.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}

protected override object SaveViewState()
{
return base.SaveViewSt ate();
}
}
}
*************** *************** *************** *

Works fine if trace="false", but fires a 2nd time if trace="true".

Do you know why it does that?

Thanks,

Tom
>
"tshad" <ts**********@f tsolutions.comw rote in message
news:Ow******** ******@TK2MSFTN GP04.phx.gbl...
>>
"Scott M." <s-***@nospam.nosp amwrote in message
news:eO******* *******@TK2MSFT NGP02.phx.gbl.. .
>>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(ISREFRE SH) was equal to nothing and on the second time
it was fired it would be "false"

At start of SaveViewState - _refreshState = False Session(ISREFRE SH) =
isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False

At start of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False
************** *************** *************** *************** *************** *************** ***
protected override object SaveViewState()
{
bool _isrefresh = false;
string _tname = "__Ticket";

FileStream fs = new
FileStream("c: \\inetpub\\wwwr oot\\staffingwo rkshop\\uploads \\tfsRefresh.tx t",
FileMode.Appen d, FileAccess.Writ e);
StreamWriter s = new StreamWriter(fs );
s.BaseStream.S eek(0,SeekOrigi n.End);
s.WriteLine( " ");
s.WriteLine("C # Version");
s.WriteLine("A t start of SaveViewState - _refreshState = " +
_refreshStat e + " Session(ISREFRE SH) = " + Session["__ISREFRES H"] + "
isRefresh = " + _isRefresh);
Session["__ISREFRES H"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewSt ate();
allStates[1] = !_refreshState;
s.WriteLine("A t End of SaveViewState - _refreshState = " + _refreshState
+ " Session(ISREFRE SH) = " + Session["__ISREFRES H"] + " 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
>>>

"tshad" <ts**********@f tsolutions.comw rote in message
news:et****** ********@TK2MSF TNGP04.phx.gbl. ..
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.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}

protected override object SaveViewState()
{
Trace.Warn(" Inside refresh.cs at SaveViewState") ;
return base.SaveViewSt ate();
}
}
}

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



Aug 26 '06 #5
I don't know why trace=true causes 2 SVS calls, but I can only assume it is
because the page class calls it the first time and the addition of the trace
info. to the page's render data calls it again.

As for my preference for doing the work in the Page_Load, rather than LVS
and SVS and the use of Sessions...

I used sessions myself back in classic ASP because they were easy and (short
of more permenant data storage), they seemed like the best overall deal in
town. But sessions, as I'm sure you know, are not the most efficient
mechanism for persisting state and also not the most reliable. Now, that
ASP.NET gives us ViewState and the Cache, I like those instead of Sessions.
Also, sessions present additional problems when applications scale to web
farms.

The actual code can be placed in several different events, but it just seems
(to me anyway) that Page_Load is the more logical choice than LVS and SVS,
since to most people it is not obvious which event fires and when (LVS
doesn't fire at all on the first page call).

Anyway, good luck!
"tshad" <ts**********@f tsolutions.comw rote in message
news:OR******** ******@TK2MSFTN GP05.phx.gbl...
"Scott M." <s-***@nospam.nosp amwrote in message
news:Ol******** ******@TK2MSFTN GP06.phx.gbl...
>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/techn..._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.Sess ionState;

namespace MyFunctions
{
public class Page : System.Web.UI.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}

protected override object SaveViewState()
{
return base.SaveViewSt ate();
}
}
}
*************** *************** *************** *

Works fine if trace="false", but fires a 2nd time if trace="true".

Do you know why it does that?

Thanks,

Tom
>>
"tshad" <ts**********@f tsolutions.comw rote in message
news:Ow******* *******@TK2MSFT NGP04.phx.gbl.. .
>>>
"Scott M." <s-***@nospam.nosp amwrote in message
news:eO****** ********@TK2MSF TNGP02.phx.gbl. ..
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
descriptio n.
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(ISREFRE SH) was equal to nothing and on the second time
it was fired it would be "false"

At start of SaveViewState - _refreshState = False Session(ISREFRE SH)
= isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False

At start of SaveViewState - _refreshState = False Session(ISREFRE SH)
= False isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False
************* *************** *************** *************** *************** *************** ****
protected override object SaveViewState()
{
bool _isrefresh = false;
string _tname = "__Ticket";

FileStream fs = new
FileStream("c :\\inetpub\\www root\\staffingw orkshop\\upload s\\tfsRefresh.t xt",
FileMode.Appe nd, FileAccess.Writ e);
StreamWrite r s = new StreamWriter(fs );
s.BaseStream. Seek(0,SeekOrig in.End);
s.WriteLine (" ");
s.WriteLine(" C# Version");
s.WriteLine(" At start of SaveViewState - _refreshState = " +
_refreshSta te + " Session(ISREFRE SH) = " + Session["__ISREFRES H"] + "
isRefresh = " + _isRefresh);
Session["__ISREFRES H"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewSt ate();
allStates[1] = !_refreshState;
s.WriteLine(" At End of SaveViewState - _refreshState = " + _refreshState
+ " Session(ISREFRE SH) = " + Session["__ISREFRES H"] + " 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
"tshad" <ts**********@f tsolutions.comw rote in message
news:et***** *********@TK2MS FTNGP04.phx.gbl ...
>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.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}
>
protected override object SaveViewState()
{
Trace.Warn( "Inside refresh.cs at SaveViewState") ;
return base.SaveViewSt ate();
}
}
}
>
>
>
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
>




Aug 26 '06 #6
"Scott M." <s-***@nospam.nosp amwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>I don't know why trace=true causes 2 SVS calls, but I can only assume it is
because the page class calls it the first time and the addition of the
trace info. to the page's render data calls it again.

As for my preference for doing the work in the Page_Load, rather than LVS
and SVS and the use of Sessions...

I used sessions myself back in classic ASP because they were easy and
(short of more permenant data storage), they seemed like the best overall
deal in town. But sessions, as I'm sure you know, are not the most
efficient mechanism for persisting state and also not the most reliable.
Now, that ASP.NET gives us ViewState and the Cache, I like those instead
of Sessions. Also, sessions present additional problems when applications
scale to web farms.
I haven't used the Cache, myself as I started using Sessions a while ago and
most of my application already depends on them. I know that this is going
to be a problem if I move this to a web farm and will need to deal that
problem at that time.

I use viewstate as well as sessions. I was using the LVS and SVS mainly as
a place to setup my session variables and check for refresh using a property
IsRefresh, just like we use IsPostBack and IsValid. This seems pretty
logical to me.

I don't want to use Page_Load if I am going to have to go to each page to
add this code and have to deal with whether we are dealing with an initial
page load or post back. I would prefer to deal with it in one place and
just call IsRefresh as needed. It seemed that LVS and SVS was a logical
place to do this or use an HTTPModule. I found these approaches in a couple
of articles on the web. The problem with the article on the HTTPModule was
that the author (Dino Espisito, I think) was creating a control on each page
that needed to do the check. This seemed like a little overkill - but the
HTTPModule might be a good place to do this, however. The LVS/SVS approach
has the problem we talked about where the LVS is not called if not a
PostBack, but works otherwise (unless Trace is enabled. This could cause a
problem if using Trace to debug).

The problem with the HTTPModule is that I can't seem to get it to run.

Thanks,

Tom
>
The actual code can be placed in several different events, but it just
seems (to me anyway) that Page_Load is the more logical choice than LVS
and SVS, since to most people it is not obvious which event fires and when
(LVS doesn't fire at all on the first page call).

Anyway, good luck!
"tshad" <ts**********@f tsolutions.comw rote in message
news:OR******** ******@TK2MSFTN GP05.phx.gbl...
>"Scott M." <s-***@nospam.nosp amwrote in message
news:Ol******* *******@TK2MSFT NGP06.phx.gbl.. .
>>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/techn..._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
LoadViewStat e 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.Sess ionState;

namespace MyFunctions
{
public class Page : System.Web.UI.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}

protected override object SaveViewState()
{
return base.SaveViewSt ate();
}
}
}
************** *************** *************** **

Works fine if trace="false", but fires a 2nd time if trace="true".

Do you know why it does that?

Thanks,

Tom
>>>
"tshad" <ts**********@f tsolutions.comw rote in message
news:Ow****** ********@TK2MSF TNGP04.phx.gbl. ..

"Scott M." <s-***@nospam.nosp amwrote in message
news:eO***** *********@TK2MS FTNGP02.phx.gbl ...
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(ISREFRE SH) was equal to nothing and on the second
time it was fired it would be "false"

At start of SaveViewState - _refreshState = False Session(ISREFRE SH)
= isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False

At start of SaveViewState - _refreshState = False Session(ISREFRE SH)
= False isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH) =
False isRefresh = False
************ *************** *************** *************** *************** *************** *****
protected override object SaveViewState()
{
bool _isrefresh = false;
string _tname = "__Ticket";

FileStream fs = new
FileStream(" c:\\inetpub\\ww wroot\\staffing workshop\\uploa ds\\tfsRefresh. txt",
FileMode.App end, FileAccess.Writ e);
StreamWrit er s = new StreamWriter(fs );
s.BaseStream .Seek(0,SeekOri gin.End);
s.WriteLine( " ");
s.WriteLine( "C# Version");
s.WriteLine( "At start of SaveViewState - _refreshState = " +
_refreshStat e + " Session(ISREFRE SH) = " + Session["__ISREFRES H"] +
" isRefresh = " + _isRefresh);
Session["__ISREFRES H"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewSt ate();
allStates[1] = !_refreshState;
s.WriteLine( "At End of SaveViewState - _refreshState = " +
_refreshStat e + " Session(ISREFRE SH) = " + Session["__ISREFRES H"] + "
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
>
>
"tshad" <ts**********@f tsolutions.comw rote in message
news:et**** **********@TK2M SFTNGP04.phx.gb l...
>>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.pag e Begin Init
>aspx.pag e End Init 0.000072 0.000072
>aspx.pag e Begin PreRender 0.000111 0.000039
>aspx.pag e End PreRender 0.000160 0.000049
> Inside refresh.cs at SaveViewState 0.000884 0.000725
>aspx.pag e Begin SaveViewState 0.001542 0.000657
> Inside refresh.cs at SaveViewState 0.001885 0.000344
>aspx.pag e End SaveViewState 0.001929 0.000044
>aspx.pag e Begin Render 0.001956 0.000027
>aspx.pag e 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.
>>
>namespac e MyFunctions
>{
> public class Page : System.Web.UI.P age
> {
> protected override void LoadViewState(o bject savedState)
> {
> base.LoadViewSt ate(savedState) ;
> }
>>
> protected override object SaveViewState()
> {
>Trace.Warn ("Inside refresh.cs at SaveViewState") ;
> return base.SaveViewSt ate();
> }
> }
>}
>>
>>
>>
>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
>>
>
>




Aug 28 '06 #7
Finally got it to work.

I used as my basic code the code from
http://jarednevans.typepad.com/techn..._techno_b.html.

The problem with this code is it doesn't take into account the Initial page
load being refreshed, which may not be a problem most of the time. But if
you do any database processing, setting session variables etc. - this could
be a problem.

What I added was the setting of a session variable to the URL and when I
enter the page, if the page is NOT a Post Backed page I compare the current
URL with what is in the Session variable. If they don't match or the
Session Variable is not there, then this is not a refresh. If it is there,
then the page was either refreshed or they just reentered the URL in the
Browsers Address line (really the same thing).

I also found that SVS (SaveViewState) is executed twice if you have tracing
on (trace="true"). You'll notice the first SVS is done between events (at
least the events that display on the trace page). I assume this one is the
being executed by Trace in some way because right after that the
SaveViewState is executed. If trace is off, you only get the SVS.

Also, the trace statement is the only thing that is in my SVS call. So
something is calling my SVS event other than the Page SVS and there are no
controls on the page.

aspx.page Begin Init
aspx.page End Init 0.000079 0.000079
aspx.page Begin PreRender 0.000116 0.000037
aspx.page End PreRender 0.000165 0.000049
Inside refresh.cs at SaveViewState 0.000880 0.000715
aspx.page Begin SaveViewState 0.001932 0.001053
Inside refresh.cs at SaveViewState 0.002330 0.000398
aspx.page End SaveViewState 0.002384 0.000054
aspx.page Begin Render 0.002411 0.000027
aspx.page End Render 0.137321 0.134909

So what I do set a variable, _firstTime, that I set to false in the 1st SVS
so that the 2nd SVS will execute normally but won't set anything.

To make this work for only specific pages, you need to add
Inherits="MyFun ctions.Page" to the Page tag on the page:

<%@ Page Language="VB" trace="true" debug="true" ContentType="te xt/html"
ResponseEncodin g="iso-8859-1" Inherits="MyFun ctions.Page" %>

or if you want this to work for every page I believe you can set it in your
Web.Config file:

<pages Inherits="MyFun ctions.Page" />

The one problem I have not been able figure out (if you are only doing this
for specific pages) is when you load a page (which would set the Session
variable) then go directly another set of pages not using this procedure.
If you then go back to the 1st page, it will set the session variable set
and think this is a refresh, when in fact it isn't.

Here is the actual code .
*************** *************** *************** *************** ***************
using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.Sess ionState;

namespace MyFunctions
{

// This program deals with 2 situations of refresh.
// Initial load of a page
// Checks to see if this is a PostBack.
// If not - Checks to see if Session("__LAST PAGEPROCESSED") matches the
current URL.
// If it does, the page has either been refreshed or re-entered in the
address line of Browser
// PostBack
// Checks to see if _refreshState that is stored in the ViewState matches
Session["__ISREFRES H"].
// if it does, we have a refresh. This is because we set the Session
variable and put the reverse
// value in the viewstate. If we refresh the page we get the old
viewstate back and the old viewstate
// value should match our new Session value. Remember we keep reversing
the values.

public class Page : System.Web.UI.P age
{
private bool _refreshState;
private bool _isRefresh;

// This variable is only important to signify whether SaveViewState is
executing for a second time
// During the same Page Life Cycle. Have only seen this when you have
tracing on (Trace="true")

private bool _firstTime = true;

public bool IsRefresh
{
get
{
if (!IsPostBack)
{
if ((Session["__INITIALREFRE SHPAGE"] != null) &&
((string)Sessio n["__INITIALREFRE SHPAGE"] ==
HttpContext.Cur rent.Request.Ur l.ToString()))
{
// Need to set this here as this is a refresh or was re-executed from
the URL line.
// In either case, the results are the same.
_isRefresh = true;
}
}
return _isRefresh;
}
}

public bool IsFirstTime
{
get
{
return _firstTime;
}
set
{
_firstTime = value;
}
}

protected override void LoadViewState(o bject savedState)
{
object[] allStates = (object[]) savedState;
base.LoadViewSt ate(allStates[0]);
_refreshState = (bool) allStates[1];
_isRefresh = _refreshState == (bool) Session["__ISREFRES H"];
Session.Remove( "__INITIALREFRE SHPAGE"); // Only there to test initial
page load refresh
}

protected override object SaveViewState()
{
object[] allStates = new object[2];
allStates[0] = base.SaveViewSt ate();

// This test is mainly when trace="true". SaveViewState will execute twice
and we only need to process this once

if (_firstTime)
{
// This only happens on the initial load of a page where there is no
LoadViewState
if (!IsPostBack)
{
Session["__INITIALREFRE SHPAGE"] =
HttpContext.Cur rent.Request.Ur l.ToString();
}
Session["__ISREFRES H"] = _refreshState;
allStates[1] = !_refreshState;
}
else
{
// We already reversed _refresh states. To do it again would put it back
the way it was.
// Need to store the _refreshState anyway as we need to return the
ViewState.

allStates[1] = !_refreshState;
}
_firstTime = false;
return allStates;
}
}
}
*************** *************** *************** *************** *************** *

In your code, you only have to check the property:

if IsRefresh...

Seems to work OK, except for the one problem I stated.

Someone mentioned a problem with the backbutton in the other program, but I
am not dealing with that here - just a refresh.

Tom

"tshad" <ts**********@f tsolutions.comw rote in message
news:ew******** ******@TK2MSFTN GP02.phx.gbl...
"Scott M." <s-***@nospam.nosp amwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>>I don't know why trace=true causes 2 SVS calls, but I can only assume it
is because the page class calls it the first time and the addition of the
trace info. to the page's render data calls it again.

As for my preference for doing the work in the Page_Load, rather than LVS
and SVS and the use of Sessions...

I used sessions myself back in classic ASP because they were easy and
(short of more permenant data storage), they seemed like the best overall
deal in town. But sessions, as I'm sure you know, are not the most
efficient mechanism for persisting state and also not the most reliable.
Now, that ASP.NET gives us ViewState and the Cache, I like those instead
of Sessions. Also, sessions present additional problems when applications
scale to web farms.

I haven't used the Cache, myself as I started using Sessions a while ago
and most of my application already depends on them. I know that this is
going to be a problem if I move this to a web farm and will need to deal
that problem at that time.

I use viewstate as well as sessions. I was using the LVS and SVS mainly
as a place to setup my session variables and check for refresh using a
property IsRefresh, just like we use IsPostBack and IsValid. This seems
pretty logical to me.

I don't want to use Page_Load if I am going to have to go to each page to
add this code and have to deal with whether we are dealing with an initial
page load or post back. I would prefer to deal with it in one place and
just call IsRefresh as needed. It seemed that LVS and SVS was a logical
place to do this or use an HTTPModule. I found these approaches in a
couple of articles on the web. The problem with the article on the
HTTPModule was that the author (Dino Espisito, I think) was creating a
control on each page that needed to do the check. This seemed like a
little overkill - but the HTTPModule might be a good place to do this,
however. The LVS/SVS approach has the problem we talked about where the
LVS is not called if not a PostBack, but works otherwise (unless Trace is
enabled. This could cause a problem if using Trace to debug).

The problem with the HTTPModule is that I can't seem to get it to run.

Thanks,

Tom
>>
The actual code can be placed in several different events, but it just
seems (to me anyway) that Page_Load is the more logical choice than LVS
and SVS, since to most people it is not obvious which event fires and
when (LVS doesn't fire at all on the first page call).

Anyway, good luck!
"tshad" <ts**********@f tsolutions.comw rote in message
news:OR******* *******@TK2MSFT NGP05.phx.gbl.. .
>>"Scott M." <s-***@nospam.nosp amwrote in message
news:Ol****** ********@TK2MSF TNGP06.phx.gbl. ..
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/techn..._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.Sess ionState;

namespace MyFunctions
{
public class Page : System.Web.UI.P age
{
protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate(savedState) ;
}

protected override object SaveViewState()
{
return base.SaveViewSt ate();
}
}
}
************* *************** *************** ***

Works fine if trace="false", but fires a 2nd time if trace="true".

Do you know why it does that?

Thanks,

Tom

"tshad" <ts**********@f tsolutions.comw rote in message
news:Ow***** *********@TK2MS FTNGP04.phx.gbl ...
>
"Scott M." <s-***@nospam.nosp amwrote in message
news:eO**** **********@TK2M SFTNGP02.phx.gb l...
>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
>viewstat e. 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(ISREFRE SH) was equal to nothing and on the second
time it was fired it would be "false"
>
At start of SaveViewState - _refreshState = False
Session(ISR EFRESH) = isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH)
= False isRefresh = False
>
At start of SaveViewState - _refreshState = False
Session(ISR EFRESH) = False isRefresh = False
At End of SaveViewState - _refreshState = False Session(ISREFRE SH)
= False isRefresh = False
>
>
*********** *************** *************** *************** *************** *************** ******
protected override object SaveViewState()
{
bool _isrefresh = false;
string _tname = "__Ticket";
>
FileStrea m fs = new
FileStream( "c:\\inetpub\\w wwroot\\staffin gworkshop\\uplo ads\\tfsRefresh .txt",
FileMode.Ap pend, FileAccess.Writ e);
StreamWrite r s = new StreamWriter(fs );
s.BaseStrea m.Seek(0,SeekOr igin.End);
s.WriteLine (" ");
s.WriteLine ("C# Version");
s.WriteLine ("At start of SaveViewState - _refreshState = " +
_refreshSta te + " Session(ISREFRE SH) = " + Session["__ISREFRES H"] +
" isRefresh = " + _isRefresh);
Session["__ISREFRES H"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewSt ate();
allStates[1] = !_refreshState;
s.WriteLine ("At End of SaveViewState - _refreshState = " +
_refreshSta te + " Session(ISREFRE SH) = " + Session["__ISREFRES H"] + "
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
>>
>>
>"tshad" <ts**********@f tsolutions.comw rote in message
>news:et*** ***********@TK2 MSFTNGP04.phx.g bl...
>>>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
>>>executin g 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.pa ge Begin Init
>>aspx.pa ge End Init 0.000072 0.000072
>>aspx.pa ge Begin PreRender 0.000111 0.000039
>>aspx.pa ge End PreRender 0.000160 0.000049
>> Inside refresh.cs at SaveViewState 0.000884 0.000725
>>aspx.pa ge Begin SaveViewState 0.001542 0.000657
>> Inside refresh.cs at SaveViewState 0.001885 0.000344
>>aspx.pa ge End SaveViewState 0.001929 0.000044
>>aspx.pa ge Begin Render 0.001956 0.000027
>>aspx.pa ge 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.
>>>
>>namespa ce MyFunctions
>>{
>> public class Page : System.Web.UI.P age
>> {
>> protected override void LoadViewState(o bject savedState)
>> {
>> base.LoadViewSt ate(savedState) ;
>> }
>>>
>> protected override object SaveViewState()
>> {
>>Trace.War n("Inside refresh.cs at SaveViewState") ;
>> return base.SaveViewSt ate();
>> }
>> }
>>}
>>>
>>>
>>>
>>I am still getting the same results.
>>>
>>It calls the SaveViewState twice. This is not what the
>>documenta tions say (at least not that I can find).
>>>
>>Am I missing something?
>>>
>>Thanks,
>>>
>>Tom
>>>
>>
>>
>
>




Aug 29 '06 #8

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

Similar topics

1
1707
by: acharyaks | last post by:
Hi Guru, the aspx page code executing behind the controls and functions executing twice. How can I avoid running the code behind the controls (for example this vanishes the control box text previously entered) when the postback method is triggered Page_Load{ If !(IsPstback then){ <Please tell me what I have to do to stop executing code
1
1531
by: T Jones | last post by:
Hello everybody! I hope someone can give hint on this, I'm very confused right now. Our program runs on a DB2-AS/400-System, we're connecting via an EasyCom-IDAPI-BDE (Borland Database Engine)-Driver. We're now executing a prepared query with parameters (Master-Detail-Relation) and the result is fine. Executing the query a second time (with the same parameters) the resultset is EMPTY!!!!
0
3083
by: Showjumper | last post by:
Hi, I have implemented the istatemanager interface in control so that i can retrieve values from the viewstate and then update my control. The control is an image gallery and i want it to react to postbacks i.e a dropdown list of image folder choices - user selects a folder and then my control will display from that folder. However, in using the load and saveviewstate, the control doesnt work quite right. It takes 2 clicks of the button to...
2
1748
by: mart | last post by:
I am experiencing a weird problem with some buttons on my webpage. The page_load is executing twice when a button is clicked. If I replace the button for a Linkbutton the page_load only loads once. I've set the Autoeventwireup to false. Could it be a framework problem ? On other computers the
3
2447
by: Brano | last post by:
HI all, I have a problem i have a web application that was working fine and this morning when i run it and click on a button that does Reponse.Redirect to a page i get this error : Server Error in '/IPFWEb_6' Application. -------------------------------------------------------------------------------- Object reference not set to an instance of an object.
1
6949
by: puja | last post by:
hi all, I have this .aspx page for which the Page_load event occurs twice. I found out while debugging. After searching google, I tried checking with Page.Ispostback method and also had "AutoEventWireUp" = false and also run after removing this tag from page directive but still my page_load event gets called twice. I have 3 pages called Search.asp, Processing.aspx and ConfirmSearch.aspx.
0
1320
by: Dabbler | last post by:
My GridView SelectedIndexChanged seems to be firing twice each time I select a row with a CommandField. protected void RegistrantsGridView_SelectedIndexChanged( Object sender, EventArgs e ) { // setup call to details page GridViewRow row = RegistrantsGridView.SelectedRow; Label lbl = (Label)row.FindControl( "RegistrantIdLabel" ); Response.Redirect( "RegistrantDetail.aspx?mode=edit&pk=" + lbl.Text ); }
0
1589
by: Louis Somers | last post by:
Hi, I have a simple ASPX page called Default.aspx. On it (nested within allot of other stuff) I have a custom control which inherits from TableRow and implements IPostBackEventHandler. This custom control hast the following code: --------- protected override void OnInit(EventArgs e) { base.OnInit(e);
0
1078
by: Op | last post by:
Hi, In my page I set debbuging point in "onLoad" method. When I do any post back in my page, I can see, that "onLoad" method is executting twice. How can I check if the method is executing first or second time ? (I used ViewState to check it, is there any smarter solution ?)
0
8619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8555
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8824
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5831
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4336
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4571
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.