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<int, string> obj = new Dictionary<int, string>();
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>
No comments:
Post a Comment