Hidden Controls yield inconsistent behavior on Postbacks

Discussion in 'ASP .Net Web Controls' started by TWEB, May 3, 2005.

  1. TWEB

    TWEB Guest

    Hi there,

    I'm using VS2003.

    I have a hidden control:
    <input id="hdnABC" type="hidden" runat="server" value="-1">

    This control is defined on my codebehind class as:
    protected System.Web.UI.HtmlControls.HtmlInputHidden hdnABC;

    This hidden element is manipulated by Javascript on the client. For the
    purpose of this discussion, let's say the Javascript changes the element
    value from -1 to 1.
    <script type="text/javascript">
    document.getElementbyid("hdnABC").value = 1;
    </script>

    I have a "Save" button which does a postback:
    <asp:button id="btnSave" runat="server" Text="Save"></asp:button>

    private void btnSave_Click(object sender, System.EventArgs e)
    {
    saveABC();
    }

    When I click Save button from the client, I'm having trouble with
    referencing the hidden form element.

    Referencing the server control in this fashion:
    int foo = System.Convert.ToInt32(hdnABC.Value);
    yields a different result than:
    int bar = System.Convert.ToInt32(Request.Form.Get("hdnABC"));

    Variable "foo" yields "-1" (INCORRECT)
    Variable "bar" yields "1" (CORRECT)

    Why is it that hdnABC.Value isn't in sync with Request.Form.Get("hdnABC")???

    Please post replies directly to newsgroup.

    Thanks
    TWEB, May 3, 2005
    #1
    1. Advertising

  2. TWEB

    Brock Allen Guest

    Hmm, odd. I don't know why it shouldn't work. If you don't get very far down
    this path, look into Page.RegisterHiddenField as a workaround.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hi there,
    >
    > I'm using VS2003.
    >
    > I have a hidden control: <input id="hdnABC" type="hidden"
    > runat="server" value="-1">
    >
    > This control is defined on my codebehind class as: protected
    > System.Web.UI.HtmlControls.HtmlInputHidden hdnABC;
    >
    > This hidden element is manipulated by Javascript on the client. For
    > the
    > purpose of this discussion, let's say the Javascript changes the
    > element
    > value from -1 to 1.
    > <script type="text/javascript">
    > document.getElementbyid("hdnABC").value = 1;
    > </script>
    > I have a "Save" button which does a postback:
    > <asp:button id="btnSave" runat="server" Text="Save"></asp:button>
    > private void btnSave_Click(object sender, System.EventArgs e)
    > {
    > saveABC();
    > }
    > When I click Save button from the client, I'm having trouble with
    > referencing the hidden form element.
    >
    > Referencing the server control in this fashion:
    > int foo = System.Convert.ToInt32(hdnABC.Value);
    > yields a different result than:
    > int bar = System.Convert.ToInt32(Request.Form.Get("hdnABC"));
    > Variable "foo" yields "-1" (INCORRECT)
    > Variable "bar" yields "1" (CORRECT)
    > Why is it that hdnABC.Value isn't in sync with
    > Request.Form.Get("hdnABC")???
    >
    > Please post replies directly to newsgroup.
    >
    > Thanks
    >
    Brock Allen, May 3, 2005
    #2
    1. Advertising

  3. Hi ,

    Regarding on the symptom you described, I also feel very strange. I'm
    thinking whether this is a page specific or project specific problem. Have
    you tried creating a new page or a new web application to perform the same
    test?

    Also, below is a simple test page which run correctly at my local side, you
    can also have a test on your side to see whether it is environment specific:

    =========aspx============

    <HTML>
    <HEAD>
    <title>hidden</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema"
    content="http://schemas.microsoft.com/intellisense/ie5">
    <script language="jscript">
    function setValue()
    {
    document.getElementById("hdValue").value = "-1";
    }
    </script>
    </HEAD>
    <body onload="setValue()">
    <form id="Form1" method="post" runat="server">

    <INPUT type="hidden" runat="server" id="hdValue" value="1">
    <BR>
    <asp:Button id="btnPost" runat="server" Text="Post Back"></asp:Button>
    </form>
    </body>
    </HTML>
    ================code behind======
    public class hidden : System.Web.UI.Page
    {
    protected System.Web.UI.HtmlControls.HtmlInputHidden hdValue;
    protected System.Web.UI.WebControls.Button btnPost;

    private void Page_Load(object sender, System.EventArgs e)
    {
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }

    private void InitializeComponent()
    {
    this.btnPost.Click += new System.EventHandler(this.btnPost_Click);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void btnPost_Click(object sender, System.EventArgs e)
    {
    Response.Write("<br>hdValue.Value: " + hdValue.Value);
    Response.Write("<br>Request.Form[\"hdValue\"]: " +
    Request.Form["hdValue"]);
    }
    }
    ================================

    If there are any other findings, please feel free to post here also.

    Thanks & Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], May 4, 2005
    #3
  4. TWEB

    TWEB Guest

    Thanks for the replies.

    Turns out that I wasn't checking IsPostBack in Page_Load where I call a
    method to Bind Data from my business objects to the controls.

    Page_Load() --> Bind Data To Controls --> button_ServerClick()

    My controls were refreshing from the business object, over-riding the
    PostBack contents before they got to the actual method that needed to
    validate contents.

    "Steven Cheng[MSFT]" wrote:

    > Hi ,
    >
    > Regarding on the symptom you described, I also feel very strange. I'm
    > thinking whether this is a page specific or project specific problem. Have
    > you tried creating a new page or a new web application to perform the same
    > test?
    >
    > Also, below is a simple test page which run correctly at my local side, you
    > can also have a test on your side to see whether it is environment specific:
    >
    > =========aspx============
    >
    > <HTML>
    > <HEAD>
    > <title>hidden</title>
    > <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" Content="C#">
    > <meta name="vs_defaultClientScript" content="JavaScript">
    > <meta name="vs_targetSchema"
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > <script language="jscript">
    > function setValue()
    > {
    > document.getElementById("hdValue").value = "-1";
    > }
    > </script>
    > </HEAD>
    > <body onload="setValue()">
    > <form id="Form1" method="post" runat="server">
    >
    > <INPUT type="hidden" runat="server" id="hdValue" value="1">
    > <BR>
    > <asp:Button id="btnPost" runat="server" Text="Post Back"></asp:Button>
    > </form>
    > </body>
    > </HTML>
    > ================code behind======
    > public class hidden : System.Web.UI.Page
    > {
    > protected System.Web.UI.HtmlControls.HtmlInputHidden hdValue;
    > protected System.Web.UI.WebControls.Button btnPost;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > }
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > private void InitializeComponent()
    > {
    > this.btnPost.Click += new System.EventHandler(this.btnPost_Click);
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    >
    > private void btnPost_Click(object sender, System.EventArgs e)
    > {
    > Response.Write("<br>hdValue.Value: " + hdValue.Value);
    > Response.Write("<br>Request.Form[\"hdValue\"]: " +
    > Request.Form["hdValue"]);
    > }
    > }
    > ================================
    >
    > If there are any other findings, please feel free to post here also.
    >
    > Thanks & Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    >
    TWEB, May 4, 2005
    #4
  5. Thanks for your further followup.

    Glad that you've figured out the problem. If there're anything else we can
    help later, please
    feel free to post here also.

    Good Luck!

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], May 5, 2005
    #5
    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. =?Utf-8?B?Q3phciBFY2xhcmluYWw=?=

    DataSet inconsistent in behavior in different session state manage

    =?Utf-8?B?Q3phciBFY2xhcmluYWw=?=, Aug 30, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    437
    =?Utf-8?B?Q3phciBFY2xhcmluYWw=?=
    Aug 30, 2005
  2. Replies:
    0
    Views:
    433
  3. Replies:
    1
    Views:
    314
    Gabriel Genellina
    Apr 22, 2008
  4. Markus
    Replies:
    1
    Views:
    187
    Mark Hubbart
    Sep 27, 2004
  5. Michael Edgar
    Replies:
    13
    Views:
    258
    Brian Candler
    Apr 21, 2011
Loading...

Share This Page