473,410 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

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 2528
There is a way to do this by the Master Detail lists. without any loops...

"Jim Bancroft" <as******@nowhere.com> wrote in message
news:uB**************@TK2MSFTNGP15.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******@nowhere.com> wrote in message
news:uB**************@TK2MSFTNGP15.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******@jackson5.com> wrote in message
news:O$**************@TK2MSFTNGP15.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******@nowhere.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
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...
1
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...
1
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...
0
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 ...
5
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...
7
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...
2
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...
5
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...
3
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)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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,...
0
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...

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.