473,785 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically build dropdownlist box

When I select the first dropdownlist box, I need to be able to make several
dropdownlist boxes on the fly and populate them from the database. Is this
possible? If so, HOW?

--

David Fetrow
Helixpoint LLC.
da***@helixpoin t.com
Nov 18 '05 #1
1 2281
Hi Dave,

The easiest way is to add a PlaceHolder control to your page. Then, create
the dropdownlist on the fly, add the items to the ddl from the database and
finally add the ddl to the PlaceHolder. Here's the idea:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub DropDownList1_S electedIndexCha nged _
(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles DropDownList1.S electedIndexCha nged
Dim ddlData As New DropDownList
Dim dt As DataTable
Dim lstItem As ListItem
Dim dr As DataRow
dt = CreateDataSourc e()
For Each dr In dt.Rows
lstItem = New ListItem
lstItem.Text = dr.Item("String Value")
lstItem.Value = dr.Item("Intege rValue")
ddlData.Items.A dd(lstItem)
Next
PlaceHolder1.Co ntrols.Add(ddlD ata)
End Sub

Function CreateDataSourc e() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add( New DataColumn("Int egerValue", GetType(Int32)) )
dt.Columns.Add( New DataColumn("Str ingValue", GetType(String) ))
dt.Columns.Add( New DataColumn("Cur rencyValue", GetType(Double) ))
dt.Columns.Add( New DataColumn("Boo lean", GetType(Boolean )))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = (i = 4)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSour ce

<form id="Form1" method="post" runat="server">
<P>
<asp:DropDownLi st id="DropDownLis t1" runat="server" AutoPostBack="T rue">
<asp:ListItem Value="--Select--">--Select--</asp:ListItem>
<asp:ListItem Value="Items">I tems</asp:ListItem>
<asp:ListItem Value="Colours" >Colours</asp:ListItem>
</asp:DropDownLis t></P>
<P>
<asp:PlaceHolde r id="PlaceHolder 1" runat="server"> </asp:PlaceHolder ></P>
</form>

Does this help?

Ken
Microsoft MVP [ASP.NET]
Toronto
"DaveF" <df*****@geodec isions.com> wrote in message
news:Ob******** ******@tk2msftn gp13.phx.gbl...
When I select the first dropdownlist box, I need to be able to make
several
dropdownlist boxes on the fly and populate them from the database. Is this
possible? If so, HOW?

--

David Fetrow
Helixpoint LLC.
da***@helixpoin t.com


Nov 18 '05 #2

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

Similar topics

4
5483
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
2
4286
by: Duncan Welch | last post by:
Hi, I've got a fairly simple date control, that I'm creating dynamically in my page. The reason for creating it dynamically is that I want the ID to vary, depending on it's situation. The control loads on the page, and the set date is shown. The problem is, when I submit the page, the control doesn't return the date. What am I doing wrong?
1
406
by: DaveF | last post by:
When I select the first dropdownlist box, I need to be able to make several dropdownlist boxes on the fly and populate them from the database. Is this possible? If so, HOW? -- David Fetrow Helixpoint LLC. davef@helixpoint.com
4
1679
by: Shiju Poyilil | last post by:
Hello EveryBody, I have a dropdownlist with some values as .net server controls & a button , on clicking a button i need to generate the choosen contorl dynamically. I am giving a small glimpse of my code for your understanding the aspx page has dropdwon list with values as below. <asp:DropDownList id="DropDownList1" runat="server"> <asp:ListItem Value="Label">Label</asp:ListItem> <asp:ListItem...
8
3391
by: utterberg | last post by:
Hi I'm adding two dropdownlist in my webform by clicking a button. I fill the first dropdown with some values from my DB and the other one when I choose from the first dropdown. But since theese dropdownlists are created by clicking a button they no longer exists on my webform. How do I keep my dynamically created dropdown when using OnSelectedIndexChanged? Can anyone help me solve this problem? Or is there a better way of doing this?
5
1967
by: Sosh | last post by:
Hi, I can't seem to find much information on this. Please could someone explain to me how to wire up events (selectedIndexChanged) to a bunch of dynamically created controls (Dropdownlists), preferably in C#. I'm sure this should be relatively straighforward, but can't for the life of me figure out a good way of doing it. Many thanks!
0
2405
by: Mike Collins | last post by:
I someone can please help, I am about at an end in trying to figure this out. I am adding some dynamic controls to my page (I found out that I was supposed to be doing that in the oninit event, which I am). I now want to read the text/values of those controls. I have found out that I can read the values if I wait until Page_Load, but then I have the same questions showing up again (along with the new questions) and I do not want them to....
4
1492
by: imranabdulaziz | last post by:
Dear All, I am using asp.net2.0, C#, sql2005 using Visual studio 2005 Let Me explain the scenario I have checkboxlist containg 15 field. Based on no of checked field . I created dropdownlistbox and label dynamically through loop and assign id as dropdownlist + no of iteration. Now my question is how I access these control as when I am accessing one of the control I am getting nullobjectreference error stating “Object reference not set...
2
3323
by: mylog | last post by:
Hi I am having a problem of getting the value from the dynamically generated table and dropdownlist. What I am facing is, I have created a table in the aspx page and now I need to add values to the the cells of the table respectively which I have done easily but the problem is when I add the dynamically generated dropdownlist to the cell I cannot read the value from the dropdownlist. For adding the dropdownlist in the cell I have done the...
0
9485
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
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...
0
10161
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
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
8986
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
7506
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
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
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
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.