![]() |
HtmlInputHidden value set on client not getting to server postback
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').Valu e); // test - displays undefined document.getElementById('hdnDirtyFlag').Value = 'true'; alert(document.getElementById('hdnDirtyFlag').Valu e); // 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. |
RE: HtmlInputHidden value set on client not getting to server postback
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. .... "Scott" wrote: > 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').Valu e); // test - > displays undefined > document.getElementById('hdnDirtyFlag').Value = 'true'; > alert(document.getElementById('hdnDirtyFlag').Valu e); // 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. > |
| All times are GMT. The time now is 02:53 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.