Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > ViewState bug? Or am I missing something?

Reply
Thread Tools

ViewState bug? Or am I missing something?

 
 
Carlo Razzeto
Guest
Posts: n/a
 
      05-08-2007
I have a few user controls which use the view state to store the UI value.
I'm running into a bit of an odd error. Many of my controls use a hidden
value in the form to store the data. The reason for this is to seperate the
internal value from the display, and still allow users to access the control
data by using Request.Form("ControlName"). In order to populate the .Text
property, I have a UserControl.Init event handler which basically does the
following:

sub Page_Init( ... ) handles me.init
Text = Request.Form(ClientID)
end sub

The set for the text property stores this directly into the view state like
ViewState(ClientID + ":Text") = value.

In this control, I also have a PreRender event handler. This event handler
will populate the display items for the control, as well as set any
javascript etc.

Sub Page_PreRender( ... ) Handles me.prerender
txtDisplay.Text = GetDisplayText()
txtDisplay.Attributes.Add("onchange", "...")....
End Sub

What I've found by stepping through the code is the following...

When the Text property is set in the Page_Init method, the value is correct
as per the controls state when the form was posted. For some reason, when it
gets to the Page_PreRender method, all of the sudden instead of the
VeiwState containing the updated text value, it has the text value that was
set when the page was initally loaded. Why is this? It seems like
ViewState() = ... does not store values between various page events. This
seems like a rather peculier behaviour to me (which also seems to defeat the
purpose of ViewState as I understand it). Thanks for any thoughts

Carlo

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      05-11-2007
Hi,

basically ViewState isn't tracked at that point of Page lifecycle and when
you set something to viewstate at that point, it isn't marked to be dirty
for persisting (event though it exists in the viewstate collection for the
rwquest)

Try moving the code to Page_Load and see if that helps.


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net


"Carlo Razzeto" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I have a few user controls which use the view state to store the UI value.
>I'm running into a bit of an odd error. Many of my controls use a hidden
>value in the form to store the data. The reason for this is to seperate the
>internal value from the display, and still allow users to access the
>control data by using Request.Form("ControlName"). In order to populate the
>.Text property, I have a UserControl.Init event handler which basically
>does the following:
>
> sub Page_Init( ... ) handles me.init
> Text = Request.Form(ClientID)
> end sub
>
> The set for the text property stores this directly into the view state
> like ViewState(ClientID + ":Text") = value.
>
> In this control, I also have a PreRender event handler. This event handler
> will populate the display items for the control, as well as set any
> javascript etc.
>
> Sub Page_PreRender( ... ) Handles me.prerender
> txtDisplay.Text = GetDisplayText()
> txtDisplay.Attributes.Add("onchange", "...")....
> End Sub
>
> What I've found by stepping through the code is the following...
>
> When the Text property is set in the Page_Init method, the value is
> correct as per the controls state when the form was posted. For some
> reason, when it gets to the Page_PreRender method, all of the sudden
> instead of the VeiwState containing the updated text value, it has the
> text value that was set when the page was initally loaded. Why is this? It
> seems like ViewState() = ... does not store values between various page
> events. This seems like a rather peculier behaviour to me (which also
> seems to defeat the purpose of ViewState as I understand it). Thanks for
> any thoughts
>
> Carlo


 
Reply With Quote
 
 
 
 
Carlo Razzeto
Guest
Posts: n/a
 
      05-17-2007
No way to mark it dirty? This particular code needs to be in Init as the
property needs to be set prior to the main page load event being fired.
Thanks!

Carlo

----- Original Message -----
From: "Teemu Keiski" <(E-Mail Removed)>
Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingc ontrols
Sent: Friday, May 11, 2007 5:13 AM
Subject: Re: ViewState bug? Or am I missing something?


> Hi,
>
> basically ViewState isn't tracked at that point of Page lifecycle and when
> you set something to viewstate at that point, it isn't marked to be dirty
> for persisting (event though it exists in the viewstate collection for the
> rwquest)
>
> Try moving the code to Page_Load and see if that helps.
>
>
> --
> Teemu Keiski
> AspInsider, ASP.NET MVP
> http://blogs.aspadvice.com/joteke
> http://teemukeiski.net
>
>
> "Carlo Razzeto" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>I have a few user controls which use the view state to store the UI value.
>>I'm running into a bit of an odd error. Many of my controls use a hidden
>>value in the form to store the data. The reason for this is to seperate
>>the internal value from the display, and still allow users to access the
>>control data by using Request.Form("ControlName"). In order to populate
>>the .Text property, I have a UserControl.Init event handler which
>>basically does the following:
>>
>> sub Page_Init( ... ) handles me.init
>> Text = Request.Form(ClientID)
>> end sub
>>
>> The set for the text property stores this directly into the view state
>> like ViewState(ClientID + ":Text") = value.
>>
>> In this control, I also have a PreRender event handler. This event
>> handler will populate the display items for the control, as well as set
>> any javascript etc.
>>
>> Sub Page_PreRender( ... ) Handles me.prerender
>> txtDisplay.Text = GetDisplayText()
>> txtDisplay.Attributes.Add("onchange", "...")....
>> End Sub
>>
>> What I've found by stepping through the code is the following...
>>
>> When the Text property is set in the Page_Init method, the value is
>> correct as per the controls state when the form was posted. For some
>> reason, when it gets to the Page_PreRender method, all of the sudden
>> instead of the VeiwState containing the updated text value, it has the
>> text value that was set when the page was initally loaded. Why is this?
>> It seems like ViewState() = ... does not store values between various
>> page events. This seems like a rather peculier behaviour to me (which
>> also seems to defeat the purpose of ViewState as I understand it). Thanks
>> for any thoughts
>>
>> Carlo

>


 
Reply With Quote
 
Teemu Keiski
Guest
Posts: n/a
 
      05-17-2007
Hi,

Setting it after TrackViewState has occurred marks added item dirty
automatically. At page_load it is, that's why I recommended using it.

If you need it to be set just before Page_Load event, override OnLoad method
of the page where you set it before calling MyBase.OnLoad (because PAge_load
event occurs when you call base method). If you use ASP.NEt 2.0, you could
just handle PreLoad event of the Page.

See: http://www.eggheadcafe.com/articles/20051227.asp


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net





"Carlo Razzeto" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> No way to mark it dirty? This particular code needs to be in Init as the
> property needs to be set prior to the main page load event being fired.
> Thanks!
>
> Carlo
>
> ----- Original Message -----
> From: "Teemu Keiski" <(E-Mail Removed)>
> Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingc ontrols
> Sent: Friday, May 11, 2007 5:13 AM
> Subject: Re: ViewState bug? Or am I missing something?
>
>
>> Hi,
>>
>> basically ViewState isn't tracked at that point of Page lifecycle and
>> when you set something to viewstate at that point, it isn't marked to be
>> dirty for persisting (event though it exists in the viewstate collection
>> for the rwquest)
>>
>> Try moving the code to Page_Load and see if that helps.
>>
>>
>> --
>> Teemu Keiski
>> AspInsider, ASP.NET MVP
>> http://blogs.aspadvice.com/joteke
>> http://teemukeiski.net
>>
>>
>> "Carlo Razzeto" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>I have a few user controls which use the view state to store the UI
>>>value. I'm running into a bit of an odd error. Many of my controls use a
>>>hidden value in the form to store the data. The reason for this is to
>>>seperate the internal value from the display, and still allow users to
>>>access the control data by using Request.Form("ControlName"). In order to
>>>populate the .Text property, I have a UserControl.Init event handler
>>>which basically does the following:
>>>
>>> sub Page_Init( ... ) handles me.init
>>> Text = Request.Form(ClientID)
>>> end sub
>>>
>>> The set for the text property stores this directly into the view state
>>> like ViewState(ClientID + ":Text") = value.
>>>
>>> In this control, I also have a PreRender event handler. This event
>>> handler will populate the display items for the control, as well as set
>>> any javascript etc.
>>>
>>> Sub Page_PreRender( ... ) Handles me.prerender
>>> txtDisplay.Text = GetDisplayText()
>>> txtDisplay.Attributes.Add("onchange", "...")....
>>> End Sub
>>>
>>> What I've found by stepping through the code is the following...
>>>
>>> When the Text property is set in the Page_Init method, the value is
>>> correct as per the controls state when the form was posted. For some
>>> reason, when it gets to the Page_PreRender method, all of the sudden
>>> instead of the VeiwState containing the updated text value, it has the
>>> text value that was set when the page was initally loaded. Why is this?
>>> It seems like ViewState() = ... does not store values between various
>>> page events. This seems like a rather peculier behaviour to me (which
>>> also seems to defeat the purpose of ViewState as I understand it).
>>> Thanks for any thoughts
>>>
>>> Carlo

>>

>


 
Reply With Quote
 
Carlo Razzeto
Guest
Posts: n/a
 
      05-18-2007
Thanks very much!

Carlo

"Teemu Keiski" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> Setting it after TrackViewState has occurred marks added item dirty
> automatically. At page_load it is, that's why I recommended using it.
>
> If you need it to be set just before Page_Load event, override OnLoad
> method of the page where you set it before calling MyBase.OnLoad (because
> PAge_load event occurs when you call base method). If you use ASP.NEt 2.0,
> you could just handle PreLoad event of the Page.
>
> See: http://www.eggheadcafe.com/articles/20051227.asp
>
>
> --
> Teemu Keiski
> AspInsider, ASP.NET MVP
> http://blogs.aspadvice.com/joteke
> http://teemukeiski.net
>
>
>
>
>
> "Carlo Razzeto" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> No way to mark it dirty? This particular code needs to be in Init as the
>> property needs to be set prior to the main page load event being fired.
>> Thanks!
>>
>> Carlo
>>
>> ----- Original Message -----
>> From: "Teemu Keiski" <(E-Mail Removed)>
>> Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingc ontrols
>> Sent: Friday, May 11, 2007 5:13 AM
>> Subject: Re: ViewState bug? Or am I missing something?
>>
>>
>>> Hi,
>>>
>>> basically ViewState isn't tracked at that point of Page lifecycle and
>>> when you set something to viewstate at that point, it isn't marked to be
>>> dirty for persisting (event though it exists in the viewstate collection
>>> for the rwquest)
>>>
>>> Try moving the code to Page_Load and see if that helps.
>>>
>>>
>>> --
>>> Teemu Keiski
>>> AspInsider, ASP.NET MVP
>>> http://blogs.aspadvice.com/joteke
>>> http://teemukeiski.net
>>>
>>>
>>> "Carlo Razzeto" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>>I have a few user controls which use the view state to store the UI
>>>>value. I'm running into a bit of an odd error. Many of my controls use a
>>>>hidden value in the form to store the data. The reason for this is to
>>>>seperate the internal value from the display, and still allow users to
>>>>access the control data by using Request.Form("ControlName"). In order
>>>>to populate the .Text property, I have a UserControl.Init event handler
>>>>which basically does the following:
>>>>
>>>> sub Page_Init( ... ) handles me.init
>>>> Text = Request.Form(ClientID)
>>>> end sub
>>>>
>>>> The set for the text property stores this directly into the view state
>>>> like ViewState(ClientID + ":Text") = value.
>>>>
>>>> In this control, I also have a PreRender event handler. This event
>>>> handler will populate the display items for the control, as well as set
>>>> any javascript etc.
>>>>
>>>> Sub Page_PreRender( ... ) Handles me.prerender
>>>> txtDisplay.Text = GetDisplayText()
>>>> txtDisplay.Attributes.Add("onchange", "...")....
>>>> End Sub
>>>>
>>>> What I've found by stepping through the code is the following...
>>>>
>>>> When the Text property is set in the Page_Init method, the value is
>>>> correct as per the controls state when the form was posted. For some
>>>> reason, when it gets to the Page_PreRender method, all of the sudden
>>>> instead of the VeiwState containing the updated text value, it has the
>>>> text value that was set when the page was initally loaded. Why is this?
>>>> It seems like ViewState() = ... does not store values between various
>>>> page events. This seems like a rather peculier behaviour to me (which
>>>> also seems to defeat the purpose of ViewState as I understand it).
>>>> Thanks for any thoughts
>>>>
>>>> Carlo
>>>

>>

>


 
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
Errors: Failed to load viewstate. & Validation of viewstate MAC failed. sck10 ASP .Net 6 09-01-2006 05:59 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
Corrupted ViewState (Yes, another issue concerning viewstate) Ben Rush ASP .Net 2 12-05-2003 04:17 PM



Advertisments