Search This Blog

Saturday 1 September 2012

Data bind Dropdownlist in asp.net



Fill drop down list dynamically using c# in asp.net.We need to write method inside IspostBack condition to avoid multiple times binding.Below Example using Dictionary collection to fill asp.net dropdownlist,we can fetch data from database as well.

protected void Page_Load(object sender, EventArgs e)
   {
       firstName = "vinod";
       if (!IsPostBack)
       {
           fillDropdownlist();
       }
   }

   public void fillDropdownlist()
   {
       Dictionary<intstring> obj = new Dictionary<intstring>();
       obj.Add(1, "vinod");
       obj.Add(2, "hari");
       
       DropDownList1.DataValueField = "Key";
       DropDownList1.DataTextField = "Value";
       DropDownList1.DataSource = obj;
       DropDownList1.DataBind();
     DropDownList1.Items.Insert(0, "--Select--");

   }
    City : 
    <asp:DropDownList ID="DropDownList1" runat="server">                                   
    </asp:DropDownList>  

Fill dropdownlist dynamically in asp.net          
 






              

No comments:

Post a Comment