HtmlInputHidden value set on client not getting to server postback

S

Scott

I'm having a problem with maintaining a form dirty flag that is shared
between the client and server. I'm trying to use an HtmlInputHidden control
(that is set to "true" on the client when changes are made) to control the
processing on the server when the form is posted back. The trouble is, on the
client side after the initial load the value of the hidden element is
undefined, but it was set to false on the server in Page_Unload, and after
post-back, the value of the hidden element is not updated in ViewState. In
other words, its as though the client side element in HTML and the server
side element declared are not the same elements.

Here is how the element is set up:

In .aspx page:
<input id="hdnDirtyFlag" type=hidden runat="server">
This IS inside the <form></form> element.

In .aspx.cs class:
protected System.Web.UI.HtmlControls.HtmlInputHidden hdnDirtyFlag;

In .aspx.cs Page_Load, if (! IsPostBack) only:
hdnDirtyFlag.Value = "false";

In .aspx.cs LoadViewState:
sDirtyFlag = (string) ViewState["hdnDirtyFlag"];

In client-side script:
function FormDirty() {
alert(document.getElementById('hdnDirtyFlag').Value); // test -
displays undefined
document.getElementById('hdnDirtyFlag').Value = 'true';
alert(document.getElementById('hdnDirtyFlag').Value); // test -
displays "true"
}
This IS being triggered correctly from input elements using ...
onchange="FormDirty();" ...


I've tracked the value of the hidden element in the page lifecycle:

First time through:
OnInit (before base.OnInit): ""
OnInit (after base.OnInit): ""
Page_Load (! IsPostBack): "false" (as assigned)
Page_PreRender: "false"

On client in FormDirty() function before a change event: undefined
On client in FormDirty() function after a change event: "true"

At postback:
OnInit (before base.OnInit): ""
OnInit (after base.OnInit): ""
LoadViewState (before base.LoadViewState): null
LoadViewState (after base.LoadViewState): null
Page_Load (IsPostBack): "false"
Page_PreRender: "false"

Any help in understanding what's going wrong would be appreciated. My goal
is to be able to determine if the hidden element's value was set to "true" on
the client when processing the post-back on the server.
 
S

Scott

For future reference, changing the reference to the hidden field in the
client-side script to "FormName.hdnDirtyFlag.value" instead of
"document.getElementById('hdnDirtyFlag').value" fixes the problem described
here.

....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top