Hidden Controls yield inconsistent behavior on Postbacks

T

TWEB

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
 
B

Brock Allen

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

Steven Cheng[MSFT]

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

TWEB

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

Steven Cheng[MSFT]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top