473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

No Update for HtmlInputHidden -- Bug or Feature?

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
6 1427
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
by: Jiho Han | last post by:
In CreateChildControls method of my Page, I am trying to see if a hidden input field is present (by id) and if not, add it to a predefined form (by id). protected override void...
2
by: Guadala Harry | last post by:
In an aspx file I have declared a hidden field like this: <input id="hTestVal" type=hidden value="-1" runat="server"> Defined in the code-behind like this: protected...
0
by: Julius Fenata | last post by:
Okay, I have pass new value from client to server, like this: <script language="JavaScript"> function JSGenerateArticleID() { HiddenValue.value = "test"; } </script>
2
by: anony | last post by:
Maybe this feature is already out there. I guess you could write triggers to do some of this. Often when designing a database I add a start_date and end_date column to the table. The start_date...
0
by: Mały Piotruś | last post by:
Hello, (Sorry for my English, it's not my country language). I created .aspx page with HTML control: <input id=CustomersType name=CustomersType type=hidden runat=server> and I have it...
5
by: preeti13 | last post by:
i am tring to update the records it is not giving me any error going through the appilication but it is not updating the data base i am not able to find the problem if any one know about this then...
13
by: Neil | last post by:
I'm running an update query in SQL 7 from QA, and it runs forever. Has been running for 20 minutes so far! The query is quite simple: update a single field in a table, based on a join with another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.