adding a HtmlInputHidden to a HtmlForm not working

J

Jiho Han

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

Marshal Antony

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
 
J

Jiho Han

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top