dynamically populate DropDownList from Array in ASP.NET

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.
  • 2856

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 1.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-2.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.