show-notice
hide-notice

Sunday 7 July 2013

How to bind data in Dropdownlist in ASP.NET


Introduction
 

This Arttical i will explain How to bind data in Dropdownlist in ASP.NET.

Example



<%@ Page Language="C#" %>

<script runat="server">
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string[] CarArray = new string[4] {"Ford", "Honda", "BMW", "Dodge"};
        string[] AirplaneArray = new string[3] {"Boeing 777", "Boeing 747",
           "Boeing 737"};
        string[] TrainArray = new string[3] {"Bullet Train", "Amtrack", "Tram"};
       
        if (DropDownList1.SelectedValue == "Car") {
            DropDownList2.DataSource = CarArray; }
        else if (DropDownList1.SelectedValue == "Airplane") {
            DropDownList2.DataSource = AirplaneArray; }
        else {
            DropDownList2.DataSource = TrainArray;
        }
               
        DropDownList2.DataBind();
        DropDownList2.Visible = true;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("You selected <b>" +
           DropDownList1.SelectedValue.ToString() + ": " +
           DropDownList2.SelectedValue.ToString() + "</b>");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>DropDownList Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Select transportation type:<br />
        <asp:DropDownList ID="DropDownList1" runat="server"
         OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
         AutoPostBack="true">
            <asp:ListItem>Select an Item</asp:ListItem>
            <asp:ListItem>Car</asp:ListItem>
            <asp:ListItem>Airplane</asp:ListItem>
            <asp:ListItem>Train</asp:ListItem>
        </asp:DropDownList>&nbsp;
        <asp:DropDownList ID="DropDownList2" runat="server" Visible="false">
        </asp:DropDownList>
        <asp:Button ID="Button1" runat="server" Text="Select Options"
         OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com