No Update for HtmlInputHidden -- Bug or Feature?

Discussion in 'ASP .Net' started by heybrakywacky@hotmail.com, Oct 21, 2005.

  1. Guest

    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
     
    , Oct 21, 2005
    #1
    1. Advertising

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

    "" 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
    >
    >
     
    =?Utf-8?B?U3JlZWppdGggUmFt?=, Oct 21, 2005
    #2
    1. Advertising

  3. Guest

    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
     
    , Oct 21, 2005
    #3
  4. 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


    "" 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
    >
    >
     
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=, Oct 21, 2005
    #4
  5. Guest

    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
     
    , Oct 21, 2005
    #5
  6. Guest

    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
     
    , Oct 22, 2005
    #6
  7. Guest

    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

    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
     
    , Oct 24, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiho Han
    Replies:
    2
    Views:
    1,919
    Jiho Han
    Feb 11, 2004
  2. Julius Fenata

    HtmlInputHidden Control

    Julius Fenata, Jan 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    432
    Julius Fenata
    Jan 18, 2005
  3. Jack
    Replies:
    4
    Views:
    2,045
  4. Ma³y Piotruœ

    ASP.NET: HtmlInputHidden = Nothing

    Ma³y Piotruœ, Dec 4, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    633
    Ma³y Piotruœ
    Dec 4, 2005
  5. Scott
    Replies:
    1
    Views:
    589
    Scott
    Nov 26, 2004
Loading...

Share This Page