Saturday, 22 March 2014

Bind object to dropdownlist

To display the data into the drop down we need to bind the data into Dropdown, data collection might be in the form of Datatable, Dataset, Datareader, Disctionay or List object.

  Binding data in drop of list object; Create a class to bind the objects; 

it is having two property

public class location
    {    
        public int id { get; set; }    
        public string locationName { get; set; }
    }




  @Binding data into dropdown Inserting the records into list collection and bind it to  dropdow

   private void BindDropdown()    
    {         List objCLocation = new List();        
        objCLocation.Add(new location { id = 1, locationName = "India" });        
        objCLocation.Add(new location { id = 2, locationName = "US" });        
        objCLocation.Add(new location { id = 3, locationName = "UK" });         
        ddlLocation.DataSource = objCLocation;        
        ddlLocation.DataValueField = "id";        
        ddlLocation.DataTextField = "locationName";        
        ddlLocation.DataBind();    
    }



  @Calling method into page load  

protected void Page_Load(object sender, EventArgs e)    
{       
    BindDropdown();    
}


  @HTML code; Declaring the dropdown list with blank with no items;
   
          Location name :          
            @How it display in browser

No comments:

Post a Comment

Others

You will like to read it