473,729 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Link Button linkButton;
protected System.Web.UI.H tmlControls.Htm lInputHidden hdfGmt;

private void Page_Load(objec t sender, System.EventArg s e)
{
HtmlForm thisForm =
(HtmlForm)this. FindControl("We bForm1");
linkButton = new LinkButton();
linkButton.Text = "click";
linkButton.Clic k += new
EventHandler(li nkButton_Click) ;
HtmlInputHidden hdfGmt =
(HtmlInputHidde n)thisForm.Find Control("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.EnableVi ewState = true;
LiteralControl litGmt = new LiteralControl( );
litGmt.ID = "litGmt";
litGmt.Text = @"
<SCRIPT Language=""Java Script"">
<!-- hide from old browsers
var curDateTime = new Date()
document.xxxxxx .hdfGmt.value=(-(curDateTime.ge tTimezoneOffset ()/60))
document.write( document.xxxxxx .hdfGmt.value)
//-->
</SCRIPT>";
litGmt.Text =
litGmt.Text.Rep lace("xxxxxx",t hisForm.Name);
thisForm.Contro ls.Add( linkButton );
thisForm.Contro ls.Add( hdfGmt );
thisForm.Contro ls.Add( litGmt );
}
}
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
private void linkButton_Clic k(object sender, System.EventArg s
e)
{
}
}


Dec 20 '05 #1
2 3275
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******@onlin e.nospam> wrote in message
news:3k******** *************** *********@4ax.c om...


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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Link Button linkButton;
protected System.Web.UI.H tmlControls.Htm lInputHidden hdfGmt;

private void Page_Load(objec t sender, System.EventArg s e)
{
HtmlForm thisForm =
(HtmlForm)this. FindControl("We bForm1");
linkButton = new LinkButton();
linkButton.Text = "click";
linkButton.Clic k += new
EventHandler(li nkButton_Click) ;
HtmlInputHidden hdfGmt =
(HtmlInputHidde n)thisForm.Find Control("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.EnableVi ewState = true;
LiteralControl litGmt = new LiteralControl( );
litGmt.ID = "litGmt";
litGmt.Text = @"
<SCRIPT Language=""Java Script"">
<!-- hide from old browsers
var curDateTime = new Date()
document.xxxxxx .hdfGmt.value=(-(curDateTime.ge tTimezoneOffset ()/60))
document.write( document.xxxxxx .hdfGmt.value)
//-->
</SCRIPT>";
litGmt.Text =
litGmt.Text.Rep lace("xxxxxx",t hisForm.Name);
thisForm.Contro ls.Add( linkButton );
thisForm.Contro ls.Add( hdfGmt );
thisForm.Contro ls.Add( litGmt );
}
}
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
private void linkButton_Clic k(object sender, System.EventArg s
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******@onlin e.nospam>
| Newsgroups: microsoft.publi c.dotnet.framew ork.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+wu6 dOhPZUShiRIglaJ n2UPJpQrDaYnlP3 leoAjxHrHWvEeFB kDok2LlbsdUB
NZeR4Xu!o6IhmE6 ApxZidxcw7lkpQ8 UNUuPqQ8jTF63Dr mMlUSg2pKtQVAec guU3+ctoo1Oz1Sk W
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.p hx.gbl!TK2MSFTN GP08.phx.gbl!ne wsfeed00.sul.t-online.de!t-onli
ne.de!border2.n ntp.dca.giganew s.com!border1.n ntp.dca.giganew s.com!nntp.giga n
ews.com!local01 .nntp.dca.gigan ews.com!news.gi ganews.com.POST ED!not-for-mail
| Xref: TK2MSFTNGXA02.p hx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:3660 86
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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.Collecti ons;
| using System.Componen tModel;
| using System.Data;
| using System.Drawing;
| using System.Web;
| using System.Web.Sess ionState;
| using System.Web.UI;
| using System.Web.UI.W ebControls;
| using System.Web.UI.H tmlControls;
|
|
| public class WebForm1 : System.Web.UI.P age
| {
| protected System.Web.UI.W ebControls.Link Button linkButton;
| protected System.Web.UI.H tmlControls.Htm lInputHidden hdfGmt;
|
| private void Page_Load(objec t sender, System.EventArg s e)
| {
| HtmlForm thisForm =
| (HtmlForm)this. FindControl("We bForm1");
| linkButton = new LinkButton();
| linkButton.Text = "click";
| linkButton.Clic k += new
| EventHandler(li nkButton_Click) ;
| HtmlInputHidden hdfGmt =
| (HtmlInputHidde n)thisForm.Find Control("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.EnableVi ewState = true;
| LiteralControl litGmt = new LiteralControl( );
| litGmt.ID = "litGmt";
| litGmt.Text = @"
| <SCRIPT Language=""Java Script"">
| <!-- hide from old browsers
| var curDateTime = new Date()
| document.xxxxxx .hdfGmt.value=(-(curDateTime.ge tTimezoneOffset ()/60))
| document.write( document.xxxxxx .hdfGmt.value)
| //-->
| </SCRIPT>";
| litGmt.Text =
| litGmt.Text.Rep lace("xxxxxx",t hisForm.Name);
| thisForm.Contro ls.Add( linkButton );
| thisForm.Contro ls.Add( hdfGmt );
| thisForm.Contro ls.Add( litGmt );
| }
| }
| override protected void OnInit(EventArg s e)
| {
| InitializeCompo nent();
| base.OnInit(e);
| }
| private void InitializeCompo nent()
| {
| this.Load += new System.EventHan dler(this.Page_ Load);
| }
| private void linkButton_Clic k(object sender, System.EventArg s
| e)
| {
| }
| }
|
|
|
|
|
|
|
|
|

Dec 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3706
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 firstname/lastname/telephone box - when the user clicks the add button, I read the values from the form fields and record them into a hidden text input field. This part works because during debugging, I have converted the <input type=hidden> into a <input type=text>...
1
9591
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 same name "junk_array". My first field of junk_array is a input type=hidden. All the others fields in junk_array that follow are type=text. I can reference this first hidden field in IE with document.form.field.value. In, fact my form works...
2
3088
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 words, their own definitions plus an example of how the word is used in practice. It'll be all client side with - cookies? to get persistence so that the words won't disappear on me each time the page is closed (which is what happened when I
1
5590
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 "txtreferral" a value and when you uncheck the checkbox it will set the hidden field value back to nothing ..I have the clicking on 1 check box at a time down but when I try to add the code to assign the value to the hidden field I get an error in the...
2
2217
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
7446
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 string is not going to be there any more, so I need some way of storing the ID. What's the best way of doing this? The obvious thought is a hidden form field, but there doesn't seem to be a web control for this. Do I just use an ordinary HTML...
1
7084
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 action="http://cm1web1/WebSurveyComponents/script/ processform.asp" method="post">
11
7418
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 is not reset to '0' when i push the reset button. If I simply change the node from "<input type="hidden" id='IsChanged' value='0'>" to "<input type="text" id='IsChanged' value='0'>" Everything is working as expected (the value is reset to '0'...
11
3103
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 open in the page mother. As you can see the select contains multiple attribute.
0
8921
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8151
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.