Please refer this
HTML:
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="ddlCities">
</asp:DropDownList>
<br />
<br />
Insert Item at Bottom
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Button Text="Insert City" runat="server" OnClick="Insert" />
</td>
<td>
<asp:TextBox ID="txtCity" runat="server" />
</td>
</tr>
</table>
<br />
<br />
Insert Item at Top
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Button ID="Button1" Text="Insert City" runat="server" OnClick="InsertTop" />
</td>
<td>
<asp:TextBox ID="txtInsertTop" runat="server" />
</td>
</tr>
</table>
<br />
<br />
Delete Top
<asp:Button Text="Delete Top" runat="server" OnClick="DeleteTop" />
<br />
<br />
Search in DropDownList
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<asp:Button Text="Search" runat="server" OnClick="Search" />
</td>
<td>
<asp:TextBox ID="txtCityName" runat="server" Text="Nagpur" />
</td>
</tr>
</table>
</div>
</form>
C#:
protected DataTable Table
{
get
{
return (DataTable)ViewState["Table"];
}
set
{
ViewState["Table"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
PopuplateDropDownList();
}
}
private void PopuplateDropDownList()
{
DataTable dt = new DataTable();
if (this.Table == null)
{
dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Name", typeof(string)) });
dt.Rows.Add("Delhi");
dt.Rows.Add("Mumbai");
dt.Rows.Add("Kolkata");
dt.Rows.Add("Nagpur");
this.ddlCities.DataSource = dt;
this.ddlCities.DataValueField = "Name";
this.ddlCities.DataTextField = "Name";
this.ddlCities.DataBind();
}
else
{
dt = this.Table;
this.ddlCities.DataSource = dt;
this.ddlCities.DataValueField = "Name";
this.ddlCities.DataTextField = "Name";
this.ddlCities.DataBind();
}
this.Table = dt;
}
/// <summary>
/// Insert Bottom
/// </summary>
protected void Insert(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.txtCity.Text.Trim()))
{
DataTable dt = this.Table;
int rowCount = dt.Rows.Count + 1;
DataRow dr = dt.NewRow();
dr["Name"] = this.txtCity.Text.Trim();
dt.Rows.InsertAt(dr, rowCount);
this.Table = dt;
PopuplateDropDownList();
}
}
/// <summary>
/// Insert Top
/// </summary>
protected void InsertTop(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.txtInsertTop.Text.Trim()))
{
DataTable dt = this.Table;
DataRow dr = dt.NewRow();
dr["Name"] = this.txtInsertTop.Text.Trim();
dt.Rows.InsertAt(dr, 0);
this.Table = dt;
PopuplateDropDownList();
}
}
/// <summary>
/// Delete Top
/// </summary>
protected void DeleteTop(object sender, EventArgs e)
{
DataTable dt = this.Table;
if (dt.Rows.Count == 0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('No Records found')", true);
}
else
{
dt.Rows[0].Delete();
dt.AcceptChanges();
this.Table = dt;
}
PopuplateDropDownList();
}
protected void Search(object sender, EventArgs e)
{
int count = 0;
DataTable dt = this.Table;
if (dt.Rows.Count == 0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('No Records found')", true);
}
for (int i = 0; i < ddlCities.Items.Count; i++)
{
if (ddlCities.Items[i].Text == this.txtCityName.Text.Trim())
{
count = 1;
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('fount at " + i + "')", true);
}
}
if (count == 0)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('Not available')", true);
}
}
DropDownList Operation:
this.ddlCities.Items.Insert(0, new ListItem("Text", "Value"));
this.ddlCities.Items.Add(new ListItem("Text", "Value"));
this.ddlCities.Items.Remove("Value");
this.ddlCities.Items.RemoveAt(0);
Thank You.