ARTICLE

How to dynamically populate DropDownList from Array in ASP.NET

Posted by Sapna Articles | ASP.NET using VB.NET January 11, 2011
ASP.NET provides a web control, the DropDownList, which is also commonly known as combo box. It can contain multiple data members, but unlike a normal list box the users can choose only one value from this control.
 
Reader Level:

ASP.NET provides a web control, the DropDownList, which is also commonly known as combo box. It can contain multiple data members, but unlike a normal list box the users can choose only one value from this control.

This DropDownList is provided as a
Server Control in ASP.Net like many other controls. This DropDownList can be used to add data manually or even for dynamically binding with data base. Let's go ahead and look at how we dynamically generate a DropDownList control from an array.

Declaration of DropDownList:

A simple declaration for DropDownList can be done as below. 

  <asp:DropDownList ID="DropDownList1" Runat="server"></asp:DropDownList>

Example

DDL.gif

Here I create a DropDownList, add a few items display data in web controls as types of courses and when you select one of these options, another DropDownList control is dynamically generated from an array. See the code snippets for this.

<%@ Page Language="VB" %>
<
script runat="server">
   Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, _
     ByVal e As System.EventArgs)
       Dim DpArray() As String = {"Diploma in Art and Design", "Diploma in Banking", "Diploma in Acting", "Diploma in Business
Administration"
, "Diploma in Visual Communication"}
       Dim BsArray() As String = {"Bachelors in Art", "Bachelors in Commerce", "Bachelors in Science", "Bachelors in Computer
Applications"
, "Bachelors in Information Technology"}
       Dim MsArray() As String = {"Masters in Art", "Masters in Commerce", "Masters in Science", "Masters in Computer Applications",
"Masters in Information Technology"}       
       If DropDownList1.SelectedValue = "Diploma Courses" Then
          DropDownList2.DataSource = DpArray
       ElseIf DropDownList1.SelectedValue = "Bachelor's Degree Courses" Then
          DropDownList2.DataSource = BsArray
       Else
          DropDownList2.DataSource = MsArray
       End If
          DropDownList2.DataBind()
          DropDownList2.Visible = True
   End Sub
   Protected Sub Button1_Click(ByVal sender As Object, _
     ByVal e As System.EventArgs)
        Response.Write("Your selected category and course is: <b>" & _
          DropDownList1.SelectedValue.ToString() & " and " & _
          DropDownList2.SelectedValue.ToString() & "</b>")
   End
Sub
</
script>
<
html xmlns="http://www.w3.org/1999/xhtml" >
<
head id="Head1" runat="server">
   <title>ASP.NET DropDownList</title
>
</
head>
<
body>
  <form id="form1" runat="server">
    <div style="font-family: Verdana; font-size: small">
      <br />
       Courses offered by University:<br />
      <br />
      <asp:DropDownList ID="DropDownList1" Runat="server"
        OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
        AutoPostBack="true">
          <asp:ListItem>Select an Category</asp:ListItem>
          <asp:ListItem>Diploma Courses</asp:ListItem>
          <asp:ListItem>Bachelor's Degree Courses</asp:ListItem>
          <asp:ListItem>Master's Degree Courses</asp:ListItem>
      </asp:DropDownList>&nbsp;
      <asp:DropDownList ID="DropDownList2" Runat="server" Visible="false">
      </asp:DropDownList>
      <asp:Button ID="Button1" Runat="server" Text="Select Options"
    </div>
  </form
>
</
body>
</
html>

Result

DDL-1.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor