Tuesday, April 23, 2013

How to bind data to dropdownlist with arrarylist in asp.net


In previous post we have discussed on how to bind the Data in Gridview,Disable dropdownlist .In this post i will show how to bind data to dropdownlist using array list in asp.net.Here i will populate drop down for city names.
<html>
<head>
</head>
<body>
<form runat=server>
<asp:DropDownList id="ddlcity" runat="server" />
</form>
</body>
</html>
We have to used ispostback to avoid the populating drop down while every time loading the page

protected void Page_Load(Object Sender, EventArgs E) 
{
if (!IsPostBack) {
ArrayList citynames = new ArrayList();
citynames.Add ("NewYork");
citynames.Add ("Hyderabad");
citynames.Add ("Bombay");
ddlcity.DataSource = citynames;
ddlcity.DataBind();
}
}

No comments:

Bel