Dynamically add a control to web form

T

Test Message

I need to dynamically add an html control to a web form.

What I have, thus far, adds the control to the page, but only places it
after the closing "</html>" tag.

System.Web.UI.HtmlControls.HtmlInputHidden h = new HtmlInputHidden();
h.ID = "FIELD1";
h.Value = "Value1";
Page.Controls.Add (h);

Is there a way to have the control added inside the "<form>" tags?

Thanks,
Scott Natwick
 
S

Scott Natwick

I found an example that may help, but I haven't been able to get it to work.

The example suggests obtaining a reference to the Form, then adding the
control the From.

Page.FindControl("form1").Controls.Add(btnDyn)

<form id="form1" runat="server">
</form>

I tried this, but I receive an error when trying to access the Form.

"System.NullReferenceException: Object reference not set to an instance of
an object."

http://authors.aspalliance.com/wisemonk/view.aspx?id=AN021002
 
J

Jos

Test said:
I need to dynamically add an html control to a web form.

What I have, thus far, adds the control to the page, but only places
it after the closing "</html>" tag.

System.Web.UI.HtmlControls.HtmlInputHidden h = new
HtmlInputHidden(); h.ID = "FIELD1";
h.Value = "Value1";
Page.Controls.Add (h);

Is there a way to have the control added inside the "<form>" tags?

Thanks,
Scott Natwick

Just change the form tag into an HTML Control (by adding
runat="server" and id="form1".

Then use form1.Controls.Add(h) instead of Page.Controls.Add(h)
 
S

Scott Natwick

Thanks Jos for the reply!

I did try what you suggested, however I'm still having difficulty on two
counts.

First, I'm not able to add the "runat="server"". When I do, the form button
Submit does not seem function. I have an "action="http..."" in the form tag
as I need the form to submit data to another site. When I add the
runat="server" to the form tag, the Submit button does not trigger a trasfer
to the other page.

Second, I wasn't able to successfully reference the "form1" in the code. I
just receive a compile error.

Does this make sense?

Thanks again.
Scott Natwick
 
S

Scott Natwick

Thanks for your help. This might be a hack, but I think I found something
that works.

I added a label to the form, then added the new control to the label.

<asp:Label id="Label1" runat="server"></asp:Label>

System.Web.UI.HtmlControls.HtmlInputHidden h = new HtmlInputHidden();
h.ID = "FieldId";
h.Value = "Value";
Label1.Controls.Add (h);

Scott Natwick
 
C

Chris Austin

If I remember correctly, using the <asp:Label> control will render a <span>
tag to the client. If you don't want to render any html to the client and
still designate a certain place in the web you would like to dynamically add
controls to then I would use the <asp:placeHolder> control instead of the
<asp:Label> control.

-Chris
~
http://weblogs.austinspad.com/caustin
 
S

Scott Natwick

Chris,

Thank you for your suggestion -- it was a good one!.

Using the Label tag did create a <span> tag and changing to the PlaceHolder
did not.

Scott
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top