|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Hidden Controls yield inconsistent behavior on PostbacksI'm using VS2003. I have a hidden control: <input id="hdnABC" type="hidden" runat="server" value="-1"> This control is defined on my codebehind class as: protected System.Web.UI.HtmlControls.HtmlInputHidden hdnABC; This hidden element is manipulated by Javascript on the client. For the purpose of this discussion, let's say the Javascript changes the element value from -1 to 1. <script type="text/javascript"> document.getElementbyid("hdnABC").value = 1; </script> I have a "Save" button which does a postback: <asp:button id="btnSave" runat="server" Text="Save"></asp:button> private void btnSave_Click(object sender, System.EventArgs e) { saveABC(); } When I click Save button from the client, I'm having trouble with referencing the hidden form element. Referencing the server control in this fashion: int foo = System.Convert.ToInt32(hdnABC.Value); yields a different result than: int bar = System.Convert.ToInt32(Request.Form.Get("hdnABC")); Variable "foo" yields "-1" (INCORRECT) Variable "bar" yields "1" (CORRECT) Why is it that hdnABC.Value isn't in sync with Request.Form.Get("hdnABC")??? Please post replies directly to newsgroup. Thanks Hmm, odd. I don't know why it shouldn't work. If you don't get very far down
this path, look into Page.RegisterHiddenField as a workaround. -Brock DevelopMentor http://staff.develop.com/ballen Show quoteHide quote > Hi there, > > I'm using VS2003. > > I have a hidden control: <input id="hdnABC" type="hidden" > runat="server" value="-1"> > > This control is defined on my codebehind class as: protected > System.Web.UI.HtmlControls.HtmlInputHidden hdnABC; > > This hidden element is manipulated by Javascript on the client. For > the > purpose of this discussion, let's say the Javascript changes the > element > value from -1 to 1. > <script type="text/javascript"> > document.getElementbyid("hdnABC").value = 1; > </script> > I have a "Save" button which does a postback: > <asp:button id="btnSave" runat="server" Text="Save"></asp:button> > private void btnSave_Click(object sender, System.EventArgs e) > { > saveABC(); > } > When I click Save button from the client, I'm having trouble with > referencing the hidden form element. > > Referencing the server control in this fashion: > int foo = System.Convert.ToInt32(hdnABC.Value); > yields a different result than: > int bar = System.Convert.ToInt32(Request.Form.Get("hdnABC")); > Variable "foo" yields "-1" (INCORRECT) > Variable "bar" yields "1" (CORRECT) > Why is it that hdnABC.Value isn't in sync with > Request.Form.Get("hdnABC")??? > > Please post replies directly to newsgroup. > > Thanks > Hi ,
Regarding on the symptom you described, I also feel very strange. I'm thinking whether this is a page specific or project specific problem. Have you tried creating a new page or a new web application to perform the same test? Also, below is a simple test page which run correctly at my local side, you can also have a test on your side to see whether it is environment specific: =========aspx============ <HTML> <HEAD> <title>hidden</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> <script language="jscript"> function setValue() { document.getElementById("hdValue").value = "-1"; } </script> </HEAD> <body onload="setValue()"> <form id="Form1" method="post" runat="server"> <INPUT type="hidden" runat="server" id="hdValue" value="1"> <BR> <asp:Button id="btnPost" runat="server" Text="Post Back"></asp:Button> </form> </body> </HTML> ================code behind====== public class hidden : System.Web.UI.Page { protected System.Web.UI.HtmlControls.HtmlInputHidden hdValue; protected System.Web.UI.WebControls.Button btnPost; private void Page_Load(object sender, System.EventArgs e) { } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.btnPost.Click += new System.EventHandler(this.btnPost_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void btnPost_Click(object sender, System.EventArgs e) { Response.Write("<br>hdValue.Value: " + hdValue.Value); Response.Write("<br>Request.Form[\"hdValue\"]: " + Request.Form["hdValue"]); } } ================================ If there are any other findings, please feel free to post here also. Thanks & 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.) Thanks for the replies.
Turns out that I wasn't checking IsPostBack in Page_Load where I call a method to Bind Data from my business objects to the controls. Page_Load() --> Bind Data To Controls --> button_ServerClick() My controls were refreshing from the business object, over-riding the PostBack contents before they got to the actual method that needed to validate contents. Show quoteHide quote "Steven Cheng[MSFT]" wrote: > Hi , > > Regarding on the symptom you described, I also feel very strange. I'm > thinking whether this is a page specific or project specific problem. Have > you tried creating a new page or a new web application to perform the same > test? > > Also, below is a simple test page which run correctly at my local side, you > can also have a test on your side to see whether it is environment specific: > > =========aspx============ > > <HTML> > <HEAD> > <title>hidden</title> > <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> > <meta name="CODE_LANGUAGE" Content="C#"> > <meta name="vs_defaultClientScript" content="JavaScript"> > <meta name="vs_targetSchema" > content="http://schemas.microsoft.com/intellisense/ie5"> > <script language="jscript"> > function setValue() > { > document.getElementById("hdValue").value = "-1"; > } > </script> > </HEAD> > <body onload="setValue()"> > <form id="Form1" method="post" runat="server"> > > <INPUT type="hidden" runat="server" id="hdValue" value="1"> > <BR> > <asp:Button id="btnPost" runat="server" Text="Post Back"></asp:Button> > </form> > </body> > </HTML> > ================code behind====== > public class hidden : System.Web.UI.Page > { > protected System.Web.UI.HtmlControls.HtmlInputHidden hdValue; > protected System.Web.UI.WebControls.Button btnPost; > > private void Page_Load(object sender, System.EventArgs e) > { > } > > #region Web Form Designer generated code > override protected void OnInit(EventArgs e) > { > InitializeComponent(); > base.OnInit(e); > } > > private void InitializeComponent() > { > this.btnPost.Click += new System.EventHandler(this.btnPost_Click); > this.Load += new System.EventHandler(this.Page_Load); > > } > #endregion > > private void btnPost_Click(object sender, System.EventArgs e) > { > Response.Write("<br>hdValue.Value: " + hdValue.Value); > Response.Write("<br>Request.Form[\"hdValue\"]: " + > Request.Form["hdValue"]); > } > } > ================================ > > If there are any other findings, please feel free to post here also. > > Thanks & 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.) > > > Thanks for your further followup.
Glad that you've figured out the problem. If there're anything else we can help later, please feel free to post here also. Good Luck! Steven Cheng Microsoft Online Support Get Secure! www.microsoft.com/security (This posting is provided "AS IS", with no warranties, and confers no rights.)
How to get the HtmlForm element of an ASP.NET Page?
HELP! TabStrip with Multipage is not working! CreateUserWizard Control Automatic updating an ASPX page HOWTO: Bring that row back into focus ... Text Length Listbox and multiple selection 2d graphics controls Text_Changed ? Parser Error in TabStrip Control |
|||||||||||||||||||||||