Thursday, 18 July 2013

bind data to dropdownlist with arraylist in asp.net

When we working with dropdown list the basic thing we have to know about data binding concepts.As i explained in previous articles we can bind data to controls in several ways .Now i will use Generics which is Array list to bind the data to Dropdownlist.Here the example shows how to add static data to array list then bind to dropdownlist in asp.net.Based on the conditions we have to place the binding function in to page postback event.There is one more thing we need observe i:e "i have used sorting and remove option for names space"
<html>
<head>
<title>Bind Data with ArrayList in Asp.net</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:DropDownList ID="ddlArrayBind" runat="server">
</asp:DropDownList>
<asp:Button id="btnBind" runat="server" Text="Bind Data" 
    onclick="btnBind_Click" />
</form>
</body>
</html>
using System;
using System.Collections;

public partial class BindDatatoDropdownwitharraylist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
  
}
protected void btnBind_Click(object sender, EventArgs e)
{
    BindatatoDropdownWithArrayList();
}
private void BindatatoDropdownWithArrayList()
{
    ArrayList ArrayItems = new ArrayList();
    ArrayItems.Add("Chip");
    ArrayItems.Add("Mouse");
    ArrayItems.Add("CPU");
    ArrayItems.Add("HardDisk");
    ArrayItems.Add("RAM");
    ddlArrayBind.DataSource = ArrayItems;
    ddlArrayBind.DataBind();
}
}

No comments:

Post a Comment