Hidden Form Field Value not Persisting

X

xenophon

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

Bruce Barker

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

Steven Cheng[MSFT]

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 <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: Hidden Form Field Value not Persisting
| Date: Tue, 20 Dec 2005 15:16:23 -0500
| Message-ID: <[email protected]>
| 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: (e-mail address removed)
| 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)
| {
| }
| }
|
|
|
|
|
|
|
|
|
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top