Hidden Form Field Value not Persisting

Discussion in 'ASP .Net' started by xenophon, Dec 20, 2005.

  1. xenophon

    xenophon Guest

    I added a Hidden Form Field to a form in the code behind.
    The value is being set in JavaScript client-side, but it is not
    persisting to the server in the PostBack.
    I know the value is being set properly because it displays in the
    document.write method.

    Create a simple page and paste the below in the code-behind (ASP.NET
    1.1-SP1)

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;


    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.LinkButton linkButton;
    protected System.Web.UI.HtmlControls.HtmlInputHidden hdfGmt;

    private void Page_Load(object sender, System.EventArgs e)
    {
    HtmlForm thisForm =
    (HtmlForm)this.FindControl("WebForm1");
    linkButton = new LinkButton();
    linkButton.Text = "click";
    linkButton.Click += new
    EventHandler(linkButton_Click);
    HtmlInputHidden hdfGmt =
    (HtmlInputHidden)thisForm.FindControl("hdfGmt");
    if ( hdfGmt == null )
    {
    hdfGmt = new HtmlInputHidden(); // This line
    always runs. It should only run once.
    hdfGmt.ID = "hdfGmt";
    hdfGmt.Name = "hdfGmt";
    hdfGmt.Value = "0";
    hdfGmt.EnableViewState = true;
    LiteralControl litGmt = new LiteralControl();
    litGmt.ID = "litGmt";
    litGmt.Text = @"
    <SCRIPT Language=""JavaScript"">
    <!-- hide from old browsers
    var curDateTime = new Date()
    document.xxxxxx.hdfGmt.value=(-(curDateTime.getTimezoneOffset()/60))
    document.write(document.xxxxxx.hdfGmt.value)
    //-->
    </SCRIPT>";
    litGmt.Text =
    litGmt.Text.Replace("xxxxxx",thisForm.Name);
    thisForm.Controls.Add( linkButton );
    thisForm.Controls.Add( hdfGmt );
    thisForm.Controls.Add( litGmt );
    }
    }
    override protected void OnInit(EventArgs e)
    {
    InitializeComponent();
    base.OnInit(e);
    }
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);
    }
    private void linkButton_Click(object sender, System.EventArgs
    e)
    {
    }
    }
    xenophon, Dec 20, 2005
    #1
    1. Advertising

  2. xenophon

    Bruce Barker Guest

    if you dynamically create the hidden control, you must create it on
    postback aslo. also for it to automatically get the postback data you need
    to create it before onload (say in oninit)

    -- bruce (sqlwork.com)


    "xenophon" <> wrote in message
    news:...
    >
    >
    > I added a Hidden Form Field to a form in the code behind.
    > The value is being set in JavaScript client-side, but it is not
    > persisting to the server in the PostBack.
    > I know the value is being set properly because it displays in the
    > document.write method.
    >
    > Create a simple page and paste the below in the code-behind (ASP.NET
    > 1.1-SP1)
    >
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.Data;
    > using System.Drawing;
    > using System.Web;
    > using System.Web.SessionState;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    >
    >
    > public class WebForm1 : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.LinkButton linkButton;
    > protected System.Web.UI.HtmlControls.HtmlInputHidden hdfGmt;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > HtmlForm thisForm =
    > (HtmlForm)this.FindControl("WebForm1");
    > linkButton = new LinkButton();
    > linkButton.Text = "click";
    > linkButton.Click += new
    > EventHandler(linkButton_Click);
    > HtmlInputHidden hdfGmt =
    > (HtmlInputHidden)thisForm.FindControl("hdfGmt");
    > if ( hdfGmt == null )
    > {
    > hdfGmt = new HtmlInputHidden(); // This line
    > always runs. It should only run once.
    > hdfGmt.ID = "hdfGmt";
    > hdfGmt.Name = "hdfGmt";
    > hdfGmt.Value = "0";
    > hdfGmt.EnableViewState = true;
    > LiteralControl litGmt = new LiteralControl();
    > litGmt.ID = "litGmt";
    > litGmt.Text = @"
    > <SCRIPT Language=""JavaScript"">
    > <!-- hide from old browsers
    > var curDateTime = new Date()
    > document.xxxxxx.hdfGmt.value=(-(curDateTime.getTimezoneOffset()/60))
    > document.write(document.xxxxxx.hdfGmt.value)
    > //-->
    > </SCRIPT>";
    > litGmt.Text =
    > litGmt.Text.Replace("xxxxxx",thisForm.Name);
    > thisForm.Controls.Add( linkButton );
    > thisForm.Controls.Add( hdfGmt );
    > thisForm.Controls.Add( litGmt );
    > }
    > }
    > override protected void OnInit(EventArgs e)
    > {
    > InitializeComponent();
    > base.OnInit(e);
    > }
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    > }
    > private void linkButton_Click(object sender, System.EventArgs
    > e)
    > {
    > }
    > }
    >
    >
    >
    >
    >
    >
    >
    >
    Bruce Barker, Dec 20, 2005
    #2
    1. Advertising

  3. Hi Xenophon,

    From the page code you provided, you're dynamically creating the Html
    HIdden field, is that any particular requirement on this? Does the problem
    occurs if you statically declare the html field in your aspx page and
    access it in serverside code?

    If anything I missed, please feel free to post here.

    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.)

    --------------------
    | NNTP-Posting-Date: Tue, 20 Dec 2005 14:18:26 -0600
    | From: xenophon <>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet
    | Subject: Hidden Form Field Value not Persisting
    | Date: Tue, 20 Dec 2005 15:16:23 -0500
    | Message-ID: <>
    | X-Newsreader: Forte Agent 2.0/32.646
    | MIME-Version: 1.0
    | Content-Type: text/plain; charset=us-ascii
    | Content-Transfer-Encoding: 7bit
    | Lines: 85
    | X-Trace:
    sv3-qHwXBgvFXR7+wu6dOhPZUShiRIglaJn2UPJpQrDaYnlP3leoAjxHrHWvEeFBkDok2LlbsdUB
    NZeR4Xu!o6IhmE6ApxZidxcw7lkpQ8UNUuPqQ8jTF63DrmMlUSg2pKtQVAecguU3+ctoo1Oz1SkW
    5kg=
    | X-Complaints-To:
    | X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
    | X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
    | X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your
    complaint properly
    | X-Postfilter: 1.3.32
    | Path:
    TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
    ne.de!border2.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.gigan
    ews.com!local01.nntp.dca.giganews.com!news.giganews.com.POSTED!not-for-mail
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet:366086
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
    |
    |
    |
    | I added a Hidden Form Field to a form in the code behind.
    | The value is being set in JavaScript client-side, but it is not
    | persisting to the server in the PostBack.
    | I know the value is being set properly because it displays in the
    | document.write method.
    |
    | Create a simple page and paste the below in the code-behind (ASP.NET
    | 1.1-SP1)
    |
    | using System;
    | using System.Collections;
    | using System.ComponentModel;
    | using System.Data;
    | using System.Drawing;
    | using System.Web;
    | using System.Web.SessionState;
    | using System.Web.UI;
    | using System.Web.UI.WebControls;
    | using System.Web.UI.HtmlControls;
    |
    |
    | public class WebForm1 : System.Web.UI.Page
    | {
    | protected System.Web.UI.WebControls.LinkButton linkButton;
    | protected System.Web.UI.HtmlControls.HtmlInputHidden hdfGmt;
    |
    | private void Page_Load(object sender, System.EventArgs e)
    | {
    | HtmlForm thisForm =
    | (HtmlForm)this.FindControl("WebForm1");
    | linkButton = new LinkButton();
    | linkButton.Text = "click";
    | linkButton.Click += new
    | EventHandler(linkButton_Click);
    | HtmlInputHidden hdfGmt =
    | (HtmlInputHidden)thisForm.FindControl("hdfGmt");
    | if ( hdfGmt == null )
    | {
    | hdfGmt = new HtmlInputHidden(); // This line
    | always runs. It should only run once.
    | hdfGmt.ID = "hdfGmt";
    | hdfGmt.Name = "hdfGmt";
    | hdfGmt.Value = "0";
    | hdfGmt.EnableViewState = true;
    | LiteralControl litGmt = new LiteralControl();
    | litGmt.ID = "litGmt";
    | litGmt.Text = @"
    | <SCRIPT Language=""JavaScript"">
    | <!-- hide from old browsers
    | var curDateTime = new Date()
    | document.xxxxxx.hdfGmt.value=(-(curDateTime.getTimezoneOffset()/60))
    | document.write(document.xxxxxx.hdfGmt.value)
    | //-->
    | </SCRIPT>";
    | litGmt.Text =
    | litGmt.Text.Replace("xxxxxx",thisForm.Name);
    | thisForm.Controls.Add( linkButton );
    | thisForm.Controls.Add( hdfGmt );
    | thisForm.Controls.Add( litGmt );
    | }
    | }
    | override protected void OnInit(EventArgs e)
    | {
    | InitializeComponent();
    | base.OnInit(e);
    | }
    | private void InitializeComponent()
    | {
    | this.Load += new System.EventHandler(this.Page_Load);
    | }
    | private void linkButton_Click(object sender, System.EventArgs
    | e)
    | {
    | }
    | }
    |
    |
    |
    |
    |
    |
    |
    |
    |
    Steven Cheng[MSFT], Dec 21, 2005
    #3
    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. xenophon

    Hidden Form Field Value Get?

    xenophon, Dec 9, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    28,587
  2. Rick
    Replies:
    3
    Views:
    8,089
    Alexey Smirnov
    Apr 13, 2010
  3. GavMc
    Replies:
    4
    Views:
    308
    Evertjan.
    Sep 22, 2005
  4. Replies:
    6
    Views:
    201
  5. jr
    Replies:
    3
    Views:
    403
Loading...

Share This Page