Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Hidden Form Field Value not Persisting

Reply
Thread Tools

Hidden Form Field Value not Persisting

 
 
xenophon
Guest
Posts: n/a
 
      12-20-2005


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








 
Reply With Quote
 
 
 
 
Bruce Barker
Guest
Posts: n/a
 
      12-20-2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> 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)
> {
> }
> }
>
>
>
>
>
>
>
>



 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      12-21-2005
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 <(E-Mail Removed)>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Subject: Hidden Form Field Value not Persisting
| Date: Tue, 20 Dec 2005 15:16:23 -0500
| Message-ID: <(E-Mail Removed)>
| 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: http://www.velocityreviews.com/forums/(E-Mail 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!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)
| {
| }
| }
|
|
|
|
|
|
|
|
|

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
Populate Hidden field on post back and retrieve value from Hidden Field Rick ASP .Net 3 04-13-2010 05:38 PM
Select box value changes value of hidden field dvwool@ix.netcom.com Javascript 6 04-27-2006 04:38 AM
Hidden Form Field Value Get? xenophon ASP .Net 1 12-10-2005 01:04 AM
Pass hidden form field value to another form field to insert in db GavMc ASP General 4 09-22-2005 06:33 PM



Advertisments