By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,827 Members | 2,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,827 IT Pros & Developers. It's quick & easy.

No Update for HtmlInputHidden -- Bug or Feature?

P: n/a
After hours of pulling my hair out on this one, I've finally realized
that the problem I'm having in my code is due to behavior that is not
at all expected.

Using ASP.NET 1.1.4322.2032, I'm trying to update a dynamically-created
hidden control on my form, across multiple postings. My experience
suggests that once the initial version of the hidden control has been
created and populated, there is no changing that value in subsequent
post-backs. The following code illustrates this issue:

--- Code Behind ---
....
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
int hiddenVal = int.Parse(Request.Form["hiddenControl_1"]);
hiddenVal++;
HtmlInputHidden hiddenControl = new HtmlInputHidden();
hiddenControl.ID = hiddenControl.Name = "hiddenControl_1";
hiddenControl.Value = hiddenVal.ToString();

Control formControl = this.FindControl("HiddenValueUpdateForm");
formControl.Controls.Add(hiddenControl);
}
else
{
HtmlInputHidden hiddenControl = new HtmlInputHidden();
hiddenControl.ID = hiddenControl.Name = "hiddenControl_1";
hiddenControl.Value = "1";

Control formControl = this.FindControl("HiddenValueUpdateForm");
formControl.Controls.Add(hiddenControl);
}
}
....
--- Code Behind ---

--- Web Page ---
....
<body MS_POSITIONING="GridLayout">
<form id="HiddenValueUpdateForm" name="HiddenValueUpdateForm"
method="post" runat="server">
<input type="submit" runat="server" id="btnSubmit"
name="btnSubmit" value="Submit Query">
</form>
</body>
....
--- Web Page ---
You can see from the code that the expected behavior is that the hidden
field is dynamically initialized and added to the form controls on the
first visit to the page. Then the value is incremented on post-back,
then added back to the form controls.

The actual behavior is that while the control is added to the page each
time, the value is never updated past its initialization value ("1").

Is this a bug, or is there some rational explanation for this behavior?
It's certainly not the behavior I expect.

Thanks,
Kevin

Nov 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
interesting.. it could be because the name 'hiddenControl_1' alreday exists
in the FORM and you are trying to add it again in PAGE_LOAD... not sure of
exact reason..

this works if you move the PostBack code to prerender..

private void WebForm1_PreRender(object sender, EventArgs e)
{
if (this.IsPostBack)
{
int hiddenVal = int.Parse(Request.Form["hiddenControl_1"]);
hiddenVal++;
HtmlInputHidden hiddenControl =new HtmlInputHidden();
hiddenControl.ID = hiddenControl.Name = "hiddenControl_1";
hiddenControl.Value = hiddenVal.ToString();
System.Web.UI.HtmlControls.HtmlForm formControl = (HtmlForm)
this.FindControl("Form1");
formControl.Controls.Add(hiddenControl);
}
}

Just curious, if you need the Hidden Control during all postbacks , why not
use a static Hidden Control ?

<input type=hidden runat=server name="hiddenControl" >

HTH
Sreejith

"he***********@hotmail.com" wrote:
After hours of pulling my hair out on this one, I've finally realized
that the problem I'm having in my code is due to behavior that is not
at all expected.

Using ASP.NET 1.1.4322.2032, I'm trying to update a dynamically-created
hidden control on my form, across multiple postings. My experience
suggests that once the initial version of the hidden control has been
created and populated, there is no changing that value in subsequent
post-backs. The following code illustrates this issue:

--- Code Behind ---
....
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
int hiddenVal = int.Parse(Request.Form["hiddenControl_1"]);
hiddenVal++;
HtmlInputHidden hiddenControl = new HtmlInputHidden();
hiddenControl.ID = hiddenControl.Name = "hiddenControl_1";
hiddenControl.Value = hiddenVal.ToString();

Control formControl = this.FindControl("HiddenValueUpdateForm");
formControl.Controls.Add(hiddenControl);
}
else
{
HtmlInputHidden hiddenControl = new HtmlInputHidden();
hiddenControl.ID = hiddenControl.Name = "hiddenControl_1";
hiddenControl.Value = "1";

Control formControl = this.FindControl("HiddenValueUpdateForm");
formControl.Controls.Add(hiddenControl);
}
}
....
--- Code Behind ---

--- Web Page ---
....
<body MS_POSITIONING="GridLayout">
<form id="HiddenValueUpdateForm" name="HiddenValueUpdateForm"
method="post" runat="server">
<input type="submit" runat="server" id="btnSubmit"
name="btnSubmit" value="Submit Query">
</form>
</body>
....
--- Web Page ---
You can see from the code that the expected behavior is that the hidden
field is dynamically initialized and added to the form controls on the
first visit to the page. Then the value is incremented on post-back,
then added back to the form controls.

The actual behavior is that while the control is added to the page each
time, the value is never updated past its initialization value ("1").

Is this a bug, or is there some rational explanation for this behavior?
It's certainly not the behavior I expect.

Thanks,
Kevin

Nov 19 '05 #2

P: n/a
Hi Kevin, I think the problem is that you've created a new hidden field
everytime the page loads.

If you must add the control dynamically, then I'd try something like
this (could be wrong);
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{

HtmlInputHidden hidden= (HtmlInputHidden
)this.FindControl("hiddenControl_1");
//check for nulls of course
hidden.Value +=1;
}
else
{
HtmlInputHidden hiddenControl = new HtmlInputHidden();
hiddenControl.ID = hiddenControl.Name = "hiddenControl_1";
hiddenControl.Value = "1";
Controls.Add(hiddenControl);
}
}

Once you've added the control to the controls collection my
understanding is ASP.NET maintains it for you. Isn't that true?

And I wouldn't do that find the form and add to the form bit either, it
should be unnecessary and could be wrong.

Jim

Nov 19 '05 #3

P: n/a
Hi Kevin:

It is neither a bug nor a feature. It is a user error :-)

In creating dynamic controls you have to initialize them first then allow
them a chance to go through the LoadViewState of the page's lifecycle but in
the case of your example below you attempted to both initialize and retrieve
the ViewState in the same stage (namely Page_Load). If you were to separate
both steps like this you would not get the outcome that you got before:

private void InitializeComponent()
{
HtmlInputHidden hiddenControl = new HtmlInputHidden();
hiddenControl.ID = hiddenControl.Name = "hiddenControl_1";
hiddenControl.Value = "1";

Control formControl = this.FindControl("HiddenValueUpdateForm");
formControl.Controls.Add(hiddenControl);

this.Load +=new EventHandler(Page_Load);

}
private void Page_Load(object sender, System.EventArgs e)
{

if (Page.IsPostBack )
{
HtmlInputHidden input= (HtmlInputHidden)Page.FindControl
("hiddenControl_1");
if (input!=null)
{
int newVal = Convert.ToInt16 (input.Value) +1;
input.Value = newVal.ToString ();
Response.Write ("New value =" + newVal);
}
}

}
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"he***********@hotmail.com" wrote:
After hours of pulling my hair out on this one, I've finally realized
that the problem I'm having in my code is due to behavior that is not
at all expected.

Using ASP.NET 1.1.4322.2032, I'm trying to update a dynamically-created
hidden control on my form, across multiple postings. My experience
suggests that once the initial version of the hidden control has been
created and populated, there is no changing that value in subsequent
post-backs. The following code illustrates this issue:

--- Code Behind ---
....
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
int hiddenVal = int.Parse(Request.Form["hiddenControl_1"]);
hiddenVal++;
HtmlInputHidden hiddenControl = new HtmlInputHidden();
hiddenControl.ID = hiddenControl.Name = "hiddenControl_1";
hiddenControl.Value = hiddenVal.ToString();

Control formControl = this.FindControl("HiddenValueUpdateForm");
formControl.Controls.Add(hiddenControl);
}
else
{
HtmlInputHidden hiddenControl = new HtmlInputHidden();
hiddenControl.ID = hiddenControl.Name = "hiddenControl_1";
hiddenControl.Value = "1";

Control formControl = this.FindControl("HiddenValueUpdateForm");
formControl.Controls.Add(hiddenControl);
}
}
....
--- Code Behind ---

--- Web Page ---
....
<body MS_POSITIONING="GridLayout">
<form id="HiddenValueUpdateForm" name="HiddenValueUpdateForm"
method="post" runat="server">
<input type="submit" runat="server" id="btnSubmit"
name="btnSubmit" value="Submit Query">
</form>
</body>
....
--- Web Page ---
You can see from the code that the expected behavior is that the hidden
field is dynamically initialized and added to the form controls on the
first visit to the page. Then the value is incremented on post-back,
then added back to the form controls.

The actual behavior is that while the control is added to the page each
time, the value is never updated past its initialization value ("1").

Is this a bug, or is there some rational explanation for this behavior?
It's certainly not the behavior I expect.

Thanks,
Kevin

Nov 19 '05 #4

P: n/a
Ah, user error. Believe it or not, I always feel better when that's
the case. I'd rather be wrong than to have to question the reliability
of the technology I'm using. :)

So the problem is essentially that I'm instantiating the control too
late in the cycle; I need to do it earlier, so that it will fully
initialize by the time I'm ready to act on it.

Thanks for your help. I moved it to the Page.Init phase of the code,
and that did the trick.

Regards,
Kevin

Nov 19 '05 #5

P: n/a
The problem may be caused by the ViewState restoring the previous value
after you've updated it with a new one. I've had this happen in very
rare situations. You can try creating the control in the Page_PreRender
event. Add the following code to your page and insert your code to
create the control.

Private Sub Page_PreRender(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
' Add your code here
End Sub

Nov 19 '05 #6

P: n/a
Yeah, it's essentially the same thing that Phillip pointed out; the
controls hadn't been allowed to go through the LoadViewState process
before I was trying to update their values.

I ended up overriding the OnPreRender method for my page, and setting
the control values accordingly in there, after initializing the
controls themselves in the Page_Load method. That seems to be working
well.

Kevin

ms*****@hotmail.com wrote:
The problem may be caused by the ViewState restoring the previous value
after you've updated it with a new one. I've had this happen in very
rare situations. You can try creating the control in the Page_PreRender
event. Add the following code to your page and insert your code to
create the control.

Private Sub Page_PreRender(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
' Add your code here
End Sub


Nov 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.