473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Hidden Form Field Value not Persisting



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
2 3253
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Randell D. | last post by:
Folks, Perhaps someone can figure this out - this is 'the process of my script' I have a form whereby I can add multiple contacts to a single address. There is only one...
1
by: mark.reichman | last post by:
First off.. Thanks to Grant Wagner for help in a previous thread related to this one. I am at a total loss... I have multiple fields in a form with the same name. Lets call the fields with the...
2
by: Citoyen du Monde | last post by:
Trying to get some ideas on a simple javascript project (to teach myself the language). I want to develop a client-side vocabulary practice application that would allow users to enter their own...
1
by: Jim | last post by:
I have a 2 checkboxes and a hidden field..what I want to happen is that you can only click on 1 of these checkboxes at a time and when you check a checkbox it will assign the hidden field...
2
by: Ron Aronica | last post by:
I want to specify the value of an input field using the return from a function. I want to do something this this: <script> function value() { return "the value"; } </script> <form>
12
by: Alan Silver | last post by:
Hello, I have a page that gets passed an ID in the query string. It then uses this ID to pull info out of a database and populate controls on the page. When the page is posted back, the query...
1
by: mark | last post by:
Forgive me if this seems like a stupid question but I need help... I'm trying to do a simple online form that emails me the results from a few fields. Here is the code: <form...
11
by: newbie | last post by:
i have a form in which a hidden field (initial value as '0', and my javascript set it to '1' when an event is trigged). In the same form, i have a reset field. But I realized that the hidden field...
11
by: viki1967 | last post by:
Hidden field Hello my friends. This is a htm page that contains a form open in window popUp ( page daughter ). This form insert a value in the one hidden field called "tec", in other form...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.