Hey micah,
Please refer below sample.
HTML
<script type="text/javascript">
function Confirm() {
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
if (confirm("Do you want to delete data?")) {
confirm_value.value = "Yes";
} else {
confirm_value.value = "No";
}
document.forms[0].appendChild(confirm_value);
}
</script>
<div>
<asp:DropDownList runat="server" ID="ddlCountry">
</asp:DropDownList>
<asp:Button ID="btnConfirm" runat="server" OnClick="Delete" Text="Delete" OnClientClick="Confirm()" />
</div>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindList();
}
}
private void BindList()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Country FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
da.Fill(dt);
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "CustomerId";
ddlCountry.DataSource = dt;
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select Items--", "0"));
}
}
}
}
}
protected void Delete(object sender, EventArgs e)
{
string confirmValue = Request.Form["confirm_value"];
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE Country = @Country", con))
{
cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.Text);
if (confirmValue == "Yes")
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
else
{
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
}
this.BindList();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindList()
End If
End Sub
Private Sub BindList()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Country FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
da.Fill(dt)
ddlCountry.DataTextField = "Country"
ddlCountry.DataValueField = "CustomerId"
ddlCountry.DataSource = dt
ddlCountry.DataBind()
ddlCountry.Items.Insert(0, New ListItem("--Select Items--", "0"))
End Using
End Using
End Using
End Using
End Sub
Protected Sub Delete(ByVal sender As Object, ByVal e As EventArgs)
Dim confirmValue As String = Request.Form("confirm_value")
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("DELETE FROM Customers WHERE Country = @Country", con)
cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedItem.Text)
If confirmValue = "Yes" Then
Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked YES!')", True)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Else
Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('You clicked NO!')", True)
End If
Me.BindList()
End Using
End Using
End Sub
Screenshot