473,789 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically add a control to web form

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.H tmlControls.Htm lInputHidden h = new HtmlInputHidden ();
h.ID = "FIELD1";
h.Value = "Value1";
Page.Controls.A dd (h);

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

Thanks,
Scott Natwick
Nov 18 '05 #1
6 2251
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.FindContro l("form1").Cont rols.Add(btnDyn )

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

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

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

http://authors.aspalliance.com/wisem...px?id=AN021002

"Test Message" <te*********@ya hoo.com> wrote in message
news:iO******** ************@co mcast.com...
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.H tmlControls.Htm lInputHidden h = new HtmlInputHidden ();
h.ID = "FIELD1";
h.Value = "Value1";
Page.Controls.A dd (h);

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

Thanks,
Scott Natwick

Nov 18 '05 #2
Jos
Test Message wrote:
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.H tmlControls.Htm lInputHidden h = new
HtmlInputHidden (); h.ID = "FIELD1";
h.Value = "Value1";
Page.Controls.A dd (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.A dd(h)

--

Jos
Nov 18 '05 #3
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

"Jos" <jn************ *@fastmail.fm> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Test Message wrote:
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.H tmlControls.Htm lInputHidden h = new
HtmlInputHidden (); h.ID = "FIELD1";
h.Value = "Value1";
Page.Controls.A dd (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.A dd(h)

--

Jos

Nov 18 '05 #4
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.H tmlControls.Htm lInputHidden h = new HtmlInputHidden ();
h.ID = "FieldId";
h.Value = "Value";
Label1.Controls .Add (h);

Scott Natwick

"Scott Natwick" <no**********@y ahoo.com> wrote in message
news:WO******** ************@co mcast.com...
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

"Jos" <jn************ *@fastmail.fm> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Test Message wrote:
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.H tmlControls.Htm lInputHidden h = new
HtmlInputHidden (); h.ID = "FIELD1";
h.Value = "Value1";
Page.Controls.A dd (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.A dd(h)

--

Jos


Nov 18 '05 #5
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:PlaceHolde r> control instead of the
<asp:Label> control.

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

"Scott Natwick" <no**********@y ahoo.com> wrote in message
news:rP******** ************@co mcast.com...
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.H tmlControls.Htm lInputHidden h = new HtmlInputHidden ();
h.ID = "FieldId";
h.Value = "Value";
Label1.Controls .Add (h);

Scott Natwick

"Scott Natwick" <no**********@y ahoo.com> wrote in message
news:WO******** ************@co mcast.com...
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

"Jos" <jn************ *@fastmail.fm> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Test Message wrote:
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.H tmlControls.Htm lInputHidden h = new
HtmlInputHidden (); h.ID = "FIELD1";
h.Value = "Value1";
Page.Controls.A dd (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.A dd(h)

--

Jos



Nov 18 '05 #6
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

"Chris Austin" <ca*****@austin spad.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
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:PlaceHolde r> control
instead of the <asp:Label> control.

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

"Scott Natwick" <no**********@y ahoo.com> wrote in message
news:rP******** ************@co mcast.com...
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.H tmlControls.Htm lInputHidden h = new HtmlInputHidden ();
h.ID = "FieldId";
h.Value = "Value";
Label1.Controls .Add (h);

Scott Natwick

"Scott Natwick" <no**********@y ahoo.com> wrote in message
news:WO******** ************@co mcast.com...
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

"Jos" <jn************ *@fastmail.fm> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Test Message wrote:
> 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.H tmlControls.Htm lInputHidden h = new
> HtmlInputHidden (); h.ID = "FIELD1";
> h.Value = "Value1";
> Page.Controls.A dd (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.A dd(h)

--

Jos



Nov 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3262
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically load one of several controls: private void btnCategory_Click(object sender, System.EventArgs e) { Control myControl = LoadControl(DropDownList1.SelectedItem.Text + ".ascx"); PlaceHolder1.Controls.Add(myControl);
8
4317
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image button at runtime: //----- Code snippet protected System.Web.UI.WebControls.PlaceHolder ImageHolder; private void Page_Load(object sender, System.EventArgs e)
1
3024
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web page dynamically. To do this, First I including a web page (MainPage.aspx) and I made form tag to Runat=Server. I included one table tag and also made this table Runat=Server side. Second I created three Web User Controls e.g....
1
2580
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web page dynamically. To do this, First I including a web page (MainPage.aspx) and I made form tag to Runat=Server. I included one table tag and also made this table Runat=Server side. Second I created three Web User Controls e.g. wucCustomerInfo.ascx,
2
2913
by: Chad | last post by:
I have a problem that I am desperate to understand. It involves dynamically adding controls to a Table control that is built as a result of performing a database query. I am not looking to avoid the problem by avoiding the table control or resorting to databound controls that better manage state for me. I hope to understand how to solve the problem by using the Table web control and sticking to the approach of building the table at run...
1
1022
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a variable (targetId) in to the usercontrol (IntergySite.aspx) by calling its setter method. Currently, I am using if-then-else and hardcoded the User Control Object to do casting and call the setter method. Question: Is there any way I could load,...
6
3382
by: Steve Booth | last post by:
I have a web form with a button and a placeholder, the button adds a user control to the placeholder (and removes any existing controls). The user control contains a single button. I have done all the usual stuff of recreating the usercontrol in the Page Init event. The 'failure' sequence is as follows: - select web form button to display the user control - select user control button, event fires - select web form button to display...
8
2820
by: BillE | last post by:
When I create a control dynamically and it grows according to the content, the control Height property still returns the original default control height instead of the height after expanding. How can I get the height of the control (in code) after it grows? I am building a windows form dynamically, creating controls and placing them on the form. There can be dozens of controls of all types on the form. This is for dynamic...
7
6672
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe Stagner)
3
5276
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first add the pane the remove event does not get handled, though thereafter it is handled without problems. ==================================================
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.