HtmlInputHidden value set on client not getting to server postback

Discussion in 'ASP .Net Web Controls' started by Scott, Nov 23, 2004.

  1. Scott

    Scott Guest

    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.
    Scott, Nov 23, 2004
    #1
    1. Advertising

  2. Scott

    Scott Guest

    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').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.
    >
    Scott, Nov 26, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiho Han
    Replies:
    2
    Views:
    1,907
    Jiho Han
    Feb 11, 2004
  2. Julius Fenata

    HtmlInputHidden Control

    Julius Fenata, Jan 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    427
    Julius Fenata
    Jan 18, 2005
  3. Replies:
    6
    Views:
    1,930
  4. Jack
    Replies:
    4
    Views:
    2,039
  5. Ma³y Piotruœ

    ASP.NET: HtmlInputHidden = Nothing

    Ma³y Piotruœ, Dec 4, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    627
    Ma³y Piotruœ
    Dec 4, 2005
Loading...

Share This Page