Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > No Update for HtmlInputHidden -- Bug or Feature?

Reply
Thread Tools

No Update for HtmlInputHidden -- Bug or Feature?

 
 
heybrakywacky@hotmail.com
Guest
Posts: n/a
 
      10-21-2005
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?U3JlZWppdGggUmFt?=
Guest
Posts: n/a
 
      10-21-2005
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

"(E-Mail Removed)" 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
>
>

 
Reply With Quote
 
 
 
 
jdlwright@gmail.com
Guest
Posts: n/a
 
      10-21-2005
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

 
Reply With Quote
 
=?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
Guest
Posts: n/a
 
      10-21-2005
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


"(E-Mail Removed)" 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
>
>

 
Reply With Quote
 
heybrakywacky@hotmail.com
Guest
Posts: n/a
 
      10-21-2005
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

 
Reply With Quote
 
mszanto@hotmail.com
Guest
Posts: n/a
 
      10-22-2005
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

 
Reply With Quote
 
heybrakywacky@hotmail.com
Guest
Posts: n/a
 
      10-24-2005
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

http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
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
ASP.NET: HtmlInputHidden = Nothing Mały Piotruœ ASP .Net 0 12-04-2005 03:37 PM
How to get HtmlInputHidden value in a popup window? Jack ASP .Net 4 11-30-2005 02:35 AM
HtmlInputHidden Control Julius Fenata ASP .Net 0 01-18-2005 10:26 AM
HtmlInputHidden value set on client not getting to server postback Scott ASP .Net Web Controls 1 11-26-2004 06:49 PM
adding a HtmlInputHidden to a HtmlForm not working Jiho Han ASP .Net 2 02-11-2004 03:22 PM



Advertisments