By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,991 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,991 IT Pros & Developers. It's quick & easy.

adding a HtmlInputHidden to a HtmlForm not working

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <ji******@infinityinfo.com> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 18 '05 #2

P: n/a
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" <do***********@yahoo.com> wrote in message
news:el**************@tk2msftngp13.phx.gbl...
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" <ji******@infinityinfo.com> wrote in message
news:ui**************@TK2MSFTNGP09.phx.gbl...
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.


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.