Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Hidden Controls yield inconsistent behavior on Postbacks

Reply
Thread Tools

Hidden Controls yield inconsistent behavior on Postbacks

 
 
TWEB
Guest
Posts: n/a
 
      05-03-2005
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
 
Reply With Quote
 
 
 
 
Brock Allen
Guest
Posts: n/a
 
      05-03-2005
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



> 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
>




 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      05-04-2005
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.)


 
Reply With Quote
 
TWEB
Guest
Posts: n/a
 
      05-04-2005
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.

"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.)
>
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      05-05-2005
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.)

 
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
[ANN] To Yield or Not to Yield: An Inferable Question Michael Edgar Ruby 13 04-21-2011 05:55 PM
yield expression programmized-formal interpretation. (interpretationof yield expression.) castironpi@gmail.com Python 1 04-22-2008 01:32 AM
AJAX - Partial Postbacks are now Full Postbacks ?!! gregfocker@earthlink.net ASP .Net 0 01-25-2007 07:04 AM
DataSet inconsistent in behavior in different session state manage =?Utf-8?B?Q3phciBFY2xhcmluYWw=?= ASP .Net 0 08-30-2005 03:56 AM
Yield & associations (was Proc / def /yield semantics) Markus Ruby 1 09-27-2004 07:04 PM



Advertisments