adding a HtmlInputHidden to a HtmlForm not working

Discussion in 'ASP .Net' started by Jiho Han, Feb 10, 2004.

  1. Jiho Han

    Jiho Han Guest

    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 CreateChildControls()
    {
    base.CreateChildControls();

    HtmlForm form = FindControl("myForm") as HtmlForm;

    HtmlInputHidden hiddenField = form.FindControl("hiddenId") as
    HtmlInputHidden;
    if (hiddenField == null)
    {
    hiddenField = new HtmlInputHidden();
    hiddenField.Id = "hiddenId";
    form.Controls.Add(hiddenField);
    }
    // Controls.Add(hiddenField);
    }

    Now if I uncomment the last Controls.Add line, it will add the hidden field
    to the page outside of the form which is not what I want.
    Can someone point me out?
    Thanks.
    Jiho Han, Feb 10, 2004
    #1
    1. Advertising

  2. Hi Jiho,
    Your code look right to me and it worked for me for an existing web form
    and the hidden field was added
    inside the form tags.So form.Controls.Add(hiddenField); is working.Probably
    some other event code is executing for the form which make it disappear when
    you
    go through the control execution life cycle.
    Are you using Visual Studio .NET?.I tested it with Visual Studio .NET 2003.
    Hope this helps.
    Regards,
    Marshal Antony
    ..NET Developer
    http://www.dotnetmarshal.com



    "Jiho Han" <> wrote in message
    news:...
    > 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 CreateChildControls()
    > {
    > base.CreateChildControls();
    >
    > HtmlForm form = FindControl("myForm") as HtmlForm;
    >
    > HtmlInputHidden hiddenField = form.FindControl("hiddenId") as
    > HtmlInputHidden;
    > if (hiddenField == null)
    > {
    > hiddenField = new HtmlInputHidden();
    > hiddenField.Id = "hiddenId";
    > form.Controls.Add(hiddenField);
    > }
    > // Controls.Add(hiddenField);
    > }
    >
    > Now if I uncomment the last Controls.Add line, it will add the hidden

    field
    > to the page outside of the form which is not what I want.
    > Can someone point me out?
    > Thanks.
    >
    >
    Marshal Antony, Feb 11, 2004
    #2
    1. Advertising

  3. Jiho Han

    Jiho Han Guest

    After trying the same code - my code wasn't exactly as simple as the snippet
    :) - separately and confirming that it does work, I still could not get it
    to work.
    However, a little variation of the control addition did the trick and that
    is to use AddAt method.
    I guess you can insert it at any position but it probably makes sense to put
    it near the top or the bottom. Still adding to the end of the controls
    collection did not work.
    Then, I figured maybe I'll remove a couple of controls at the bottom and try
    it again. That worked. I put them back and tried again. Now everything
    works. Go figure.

    Thanks though for looking into this. Much appreciated.
    Jiho

    "Marshal Antony" <> wrote in message
    news:el$...
    > Hi Jiho,
    > Your code look right to me and it worked for me for an existing web

    form
    > and the hidden field was added
    > inside the form tags.So form.Controls.Add(hiddenField); is

    working.Probably
    > some other event code is executing for the form which make it disappear

    when
    > you
    > go through the control execution life cycle.
    > Are you using Visual Studio .NET?.I tested it with Visual Studio .NET

    2003.
    > Hope this helps.
    > Regards,
    > Marshal Antony
    > .NET Developer
    > http://www.dotnetmarshal.com
    >
    >
    >
    > "Jiho Han" <> wrote in message
    > news:...
    > > 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 CreateChildControls()
    > > {
    > > base.CreateChildControls();
    > >
    > > HtmlForm form = FindControl("myForm") as HtmlForm;
    > >
    > > HtmlInputHidden hiddenField = form.FindControl("hiddenId") as
    > > HtmlInputHidden;
    > > if (hiddenField == null)
    > > {
    > > hiddenField = new HtmlInputHidden();
    > > hiddenField.Id = "hiddenId";
    > > form.Controls.Add(hiddenField);
    > > }
    > > // Controls.Add(hiddenField);
    > > }
    > >
    > > Now if I uncomment the last Controls.Add line, it will add the hidden

    > field
    > > to the page outside of the form which is not what I want.
    > > Can someone point me out?
    > > Thanks.
    > >
    > >

    >
    >
    Jiho Han, Feb 11, 2004
    #3
    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. Julius Fenata

    HtmlInputHidden Control

    Julius Fenata, Jan 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    428
    Julius Fenata
    Jan 18, 2005
  2. Replies:
    6
    Views:
    1,933
  3. Jack
    Replies:
    4
    Views:
    2,042
  4. Ma³y Piotruœ

    ASP.NET: HtmlInputHidden = Nothing

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

Share This Page