Thursday, March 29, 2012

Please explain strange viewstate behavior...

I am dynamically adding an HtmlInputHidden element with different values.
The problem is the first time I add it with a given value and submit it then
change the value within the Page_Load event it still has the original value.
I am guessing this has something to do with viewstate. I have turned off
viewstate at the page level, but the issue still occurs. Any suggestions?

The code is basic:
HtmlInputHiddenphihAction = new HtmlInputHidden();

phihAction.ID = "hdnAction";
phihAction.Value = ((short)pactAction).ToString();
mfrmUser.Controls.Add( phihAction );

pactAction is an enumeration that changes based on action (i.e. 1=Add, 2=Edit)

Any help in understanding why it does what it does and how to work around it
would be greatly appreciated.

--
RobertRobert,

You need to check IsPostBack property befory assigning the value:

HtmlInputHidden phihAction = new HtmlInputHidden();

phihAction.ID = "hdnAction";
if (IsPostBack)
phihAction.Value = ((short)pactAction).ToString();
mfrmUser.Controls.Add( phihAction );

Eliyahu

"rgrandidier" <rgrandidier@.discussions.microsoft.com> wrote in message
news:E0B96D78-C2B6-4A46-BE08-E7E7B7FD1893@.microsoft.com...
> I am dynamically adding an HtmlInputHidden element with different values.
> The problem is the first time I add it with a given value and submit it
then
> change the value within the Page_Load event it still has the original
value.
> I am guessing this has something to do with viewstate. I have turned off
> viewstate at the page level, but the issue still occurs. Any suggestions?
> The code is basic:
> HtmlInputHidden phihAction = new HtmlInputHidden();
> phihAction.ID = "hdnAction";
> phihAction.Value = ((short)pactAction).ToString();
> mfrmUser.Controls.Add( phihAction );
> pactAction is an enumeration that changes based on action (i.e. 1=Add,
2=Edit)
> Any help in understanding why it does what it does and how to work around
it
> would be greatly appreciated.
> --
> Robert

0 comments:

Post a Comment