By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,009 Members | 2,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,009 IT Pros & Developers. It's quick & easy.

Hidden Form Field Value not Persisting

P: n/a


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)
{
}
}


Dec 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <xe******@online.nospam> wrote in message
news:3k********************************@4ax.com...


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)
{
}
}



Dec 20 '05 #2

P: n/a
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 <xe******@online.nospam>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: Hidden Form Field Value not Persisting
| Date: Tue, 20 Dec 2005 15:16:23 -0500
| Message-ID: <3k********************************@4ax.com>
| 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+wu6dOhPZUShiRIglaJn2UPJpQrDaYnlP3leoAj xHrHWvEeFBkDok2LlbsdUB
NZeR4Xu!o6IhmE6ApxZidxcw7lkpQ8UNUuPqQ8jTF63DrmMlUS g2pKtQVAecguU3+ctoo1Oz1SkW
5kg=
| X-Complaints-To: ab***@giganews.com
| 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!newsfee d00.sul.t-online.de!t-onli
ne.de!border2.nntp.dca.giganews.com!border1.nntp.d ca.giganews.com!nntp.gigan
ews.com!local01.nntp.dca.giganews.com!news.giganew s.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)
| {
| }
| }
|
|
|
|
|
|
|
|
|

Dec 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.