473,569 Members | 2,879 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to dynamically add <asp:> controls to a webpage?

Hi everyone,

I've been working with ASP.Net for about a month, and in all the samples
I've seen the authors know ahead of time how many DropDownLists or Labels
they're going to need.

My problem is that I don't always know at design time how many (for
instance) DropDownLists I'll need. As an example, one of my old asp pages
queries a table and loops through the resulting recordset, adding one
DropDownList per entry and populating it with the values from another
recordset.

I'm not sure how best to do this sort of "dynamic control creation" in
asp.net and was wondering if anyone could post a bit of sample code or nudge
me in the right direction? Should I / must I do this sort of thing in the
aspx file or can it be done in the code-behind file too? I'm not sure.

Thanks for the help.

-Jim
Nov 19 '05 #1
4 2537
There is a way to do this by the Master Detail lists. without any loops...

"Jim Bancroft" <as******@nowhe re.com> wrote in message
news:uB******** ******@TK2MSFTN GP15.phx.gbl...
Hi everyone,

I've been working with ASP.Net for about a month, and in all the samples I've seen the authors know ahead of time how many DropDownLists or Labels
they're going to need.

My problem is that I don't always know at design time how many (for
instance) DropDownLists I'll need. As an example, one of my old asp pages
queries a table and loops through the resulting recordset, adding one
DropDownList per entry and populating it with the values from another
recordset.

I'm not sure how best to do this sort of "dynamic control creation" in
asp.net and was wondering if anyone could post a bit of sample code or nudge me in the right direction? Should I / must I do this sort of thing in the
aspx file or can it be done in the code-behind file too? I'm not sure.

Thanks for the help.

-Jim

Nov 19 '05 #2

OnPageLoad

Dim MyDropDown as DropDownList
Dim i as integer

For i = 0 to 10
MyDropDown = new DropDownList()

MyDropDown.id = "ddl" & i

me.controls.add (myDropDown)

Next

You can probably get the hang of it from there.

-Cj

"Jim Bancroft" <as******@nowhe re.com> wrote in message
news:uB******** ******@TK2MSFTN GP15.phx.gbl...
Hi everyone,

I've been working with ASP.Net for about a month, and in all the
samples I've seen the authors know ahead of time how many DropDownLists or
Labels they're going to need.

My problem is that I don't always know at design time how many (for
instance) DropDownLists I'll need. As an example, one of my old asp pages
queries a table and loops through the resulting recordset, adding one
DropDownList per entry and populating it with the values from another
recordset.

I'm not sure how best to do this sort of "dynamic control creation" in
asp.net and was wondering if anyone could post a bit of sample code or
nudge me in the right direction? Should I / must I do this sort of thing
in the aspx file or can it be done in the code-behind file too? I'm not
sure.

Thanks for the help.

-Jim

Nov 19 '05 #3
Thanks very much.....

I don't suppose if you know how to place these items after they're created?
Right now I'm getting a runtime error that says "Control 'ddl0' of type
'DropDownList' must be placed inside a form tag with runat=server"

-Jim
"CJ Taylor" <jj******@jacks on5.com> wrote in message
news:O$******** ******@TK2MSFTN GP15.phx.gbl...

OnPageLoad

Dim MyDropDown as DropDownList
Dim i as integer

For i = 0 to 10
MyDropDown = new DropDownList()

MyDropDown.id = "ddl" & i

me.controls.add (myDropDown)

Next

You can probably get the hang of it from there.

-Cj

Nov 19 '05 #4
Jim Bancroft < as******@nowher e.com > wrote:
Thanks very much.....
I don't suppose if you know how to place these items after they're
created? Right now I'm getting a runtime error that says "Control
'ddl0' of type 'DropDownList' must be placed inside a form tag with
runat=server" -Jim

You'll probably want to create a placeholder control in the designer for
your aspx page. Then you dynamically add controls to the Controls
collection of that placeholder.

Be warned that dynamic controls can be quite tricky, and although their
behavior is very predictable it can sometimes be very hard to track down
bugs, especially with postback/viewstate. I highly reccomend you read this
article, which despite dealing with Viewstate more than anything else is
nonetheless very helpful in understanding the page life cycle, which is
crucial to dynamic controls:

http://msdn.microsoft.com/library/de.../viewstate.asp
Nov 19 '05 #5

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

Similar topics

0
1249
by: Martin Jacobsson | last post by:
Hi! I have just installed VS.NET 2003, I have problem with intelisense in <asp:>-tags in aspx-files. I prevoiusly ran VS.NET and had no problems with this. Is it some property on the project or in the environment that I am not aware of? br Martin Jacobsson
1
1189
by: Andy Crawford | last post by:
When I ran my working ASP.NET program only the <a anchors showed up. All of the <asp: ...> controls did not. What's going on? (XP pro, VS.NET 2002, IIS 5.1) I have reinstalled IIS and VS.NET. The app works, it just won't display the asp server controls. I don't know if this is related or not, but I've not been able to access my web...
1
1642
by: s8chem | last post by:
I'm working with an XSL document that renders ASP.NET Server Controls. In order to do this, I have to declare an "asp" namespace within the XSL document. This namespace is the same as when you see a line like this: <asp:label> in an ASPX page. My question is where is this namespace declared? In an XSL document you see a line such as:...
0
1327
by: Bryan Donaldson | last post by:
I have a table on my web form, declared like this: <asp:table id="tblOverrides" runat="server" enableviewstate=true" cssclass="clsTable"> <asp:tablerow cssclass="clsTblHeader"> <asp:tablecell Text="label" id="hdrLabel"></asp:tablecell"> <asp:tablecell Text="ddl" id="hdrDDL"></asp:tablecell"> </asp:tablerow> </asp:table>...
5
2089
by: Ken Dopierala Jr. | last post by:
Hi, This is just a query about what people use most. Up until today I've been using <asp:Table> tags to build my tables. We just outsourced our HTML design to a local guy and when I got it back today I was working with the html <table> <tr> <td> etc. What a difference that makes, server controls are even automatically declared in the code...
7
3567
by: Venus | last post by:
Hello, I am trying to generate a dynamic form at runtime and would like to do it using "<asp: ..." form elements as follows Build up the string that is placed somewhere in the HTML code the same way like regular input fields can. strForm = "<form name=""myForm"" runat=""server"">" & vbCrLf strForm += "<asp:button name=""myName"" .......
2
1122
by: AnandaSim | last post by:
Hi All, I'd like to use a static XML file as a source of data to any of the <asp: list controls. I have produced the .xml file, inferred a schema - .xsd, generated a dataset .vb. How do I complete the process and use the dataset in a vb.net asp.net file? I've been having a peek at the code that VS 2003 generates for creating an ADO.NET...
5
2207
by: Alex Maghen | last post by:
I frequently find myself wanting to insert some basic client-side JavaScript functions in the page of an ASPX of mine. But I find it so frustrating that I have to actually contruct my JavaScript in C# on the server-side and render it to the client because I can't use the IDs of my controls as they are written in the HTML page itself. Rather, I...
3
1335
by: henk | last post by:
Hey, Question, how can i create client-side javascript that refers to a asp.net webcontrol. For example set the focus of a textbox lik this. <asp: TextBox ID="Textbox1" /> (client side) <script javascript> Textbox1.focus();
0
7703
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...
0
7619
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7930
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. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
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...
1
5514
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...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
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.