[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.buildingcontrols

ViewState bug? Or am I missing something?

Carlo Razzeto

5/8/2007 4:06:00 PM

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

4 Answers

Teemu Keiski

5/11/2007 9:14:00 AM

0

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....
http://teemu...


"Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
news:BCC9016B-24C3-4663-98B6-8C18817AB627@microsoft.com...
>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

Carlo Razzeto

5/17/2007 3:19:00 AM

0

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" <joteke@aspalliance.com>
Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
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....
> http://teemu...
>
>
> "Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
> news:BCC9016B-24C3-4663-98B6-8C18817AB627@microsoft.com...
>>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
>

Teemu Keiski

5/17/2007 8:50:00 AM

0

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/20...


--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice....
http://teemu...





"Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
news:F506F8E6-8036-43D6-BE91-635EBF0BA412@microsoft.com...
> 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" <joteke@aspalliance.com>
> Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
> 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....
>> http://teemu...
>>
>>
>> "Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
>> news:BCC9016B-24C3-4663-98B6-8C18817AB627@microsoft.com...
>>>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
>>
>

Carlo Razzeto

5/18/2007 6:48:00 PM

0

Thanks very much!

Carlo

"Teemu Keiski" <joteke@aspalliance.com> wrote in message
news:03A5BC79-2DF9-4405-BA2C-70F653B4EB16@microsoft.com...
> 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/20...
>
>
> --
> Teemu Keiski
> AspInsider, ASP.NET MVP
> http://blogs.aspadvice....
> http://teemu...
>
>
>
>
>
> "Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
> news:F506F8E6-8036-43D6-BE91-635EBF0BA412@microsoft.com...
>> 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" <joteke@aspalliance.com>
>> Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
>> 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....
>>> http://teemu...
>>>
>>>
>>> "Carlo Razzeto" <crazzeto@hotmail.com> wrote in message
>>> news:BCC9016B-24C3-4663-98B6-8C18817AB627@microsoft.com...
>>>>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
>>>
>>
>