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