I have bound DropDownList from DataBase. When CheckBox is checked then Selected Value of DropDownList will be deleted.
HTML:
<form id="form2" runat="server">
<div>
<asp:DropDownList ID="ddlCity" runat="server">
</asp:DropDownList>
<br />
<asp:CheckBox ID="chkDelete" Text="Delete" runat="server" />
<br />
<asp:Button ID="btnUpdate" runat="server" OnClick="Delete" Text="Delete" />
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetCities();
}
}
private void DeleteSelected(string city)
{
// fire your delete query;
this.GetCities();
}
protected void Delete(object sender, EventArgs e)
{
if (this.chkDelete.Checked)
{
this.DeleteSelected(this.ddlCity.SelectedItem.Text);
}
}
private void GetCities()
{
string constr = ConfigurationManager.ConnectionStrings["ConString2"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT DISTINCT (City) FROM City", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
this.ddlCity.DataTextField = "City";
this.ddlCity.DataValueField = "City";
this.ddlCity.DataSource = ds;
this.ddlCity.DataBind();
}
}
}
}
Thank You.