Pages

Friday, 17 February 2012

Dynamicaly generate DropDownList Control from Array(C# Code)

Here is a block of C# code which generates DropDownList control from an array when the button clicks.

protected void Button1_Click(object sender,EventArgs e)
{
        //Defines car Array
        string CarArray = new string { "Ford" , "Honda" , "BMW" , "Maruthi" };
        //Defines car AirplaneArray
        string AirplaneArray = new string { " Boing 777 " , " Boing 747 " , " Boing 737 " };
        //Defines car  Train Array
        string TrainArray = new string { " Bullet Train" , " Amtrack " , " Tram " };
        if ( DropDownList1.SelectedValue = =  "Car" )
        {

               //Sets CarArray as datasource

               DropDownList2.DataSource = CarArray ;
        }
        else if (DropDownList1.SelectedValue = = "Airplane" )
        {
                //Sets AirplaneArray as datasource
                DropDownList2.DataSource = AirplaneArray ;
        }
        else
        {
               //Sets  TrainArray Array as datasource
               DropDownlList2.DataSource = TrainArray ;
        }
        //Binds the datasource with the control and generate the dropdown list
        DropDownList2.DataBind( );
        DropDownList2.Visible = true ;
}

No comments:

Post a Comment