ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Execute code based on Server Side Yes No Confirmation Box on Button Click using C# and VB.Net in ASP.Net <p>Hey <a class="username" rel="micah">micah</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;script type=&#34;text/javascript&#34;&gt; function Confirm() { var confirm_value = document.createElement(&#34;INPUT&#34;); confirm_value.type = &#34;hidden&#34;; confirm_value.name = &#34;confirm_value&#34;; if (confirm(&#34;Do you want to delete data?&#34;)) { confirm_value.value = &#34;Yes&#34;; } else { confirm_value.value = &#34;No&#34;; } document.forms[0].appendChild(confirm_value); } &lt;/script&gt; &lt;div&gt; &lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlCountry&#34;&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button ID=&#34;btnConfirm&#34; runat=&#34;server&#34; OnClick=&#34;Delete&#34; Text=&#34;Delete&#34; OnClientClick=&#34;Confirm()&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.BindList(); } } private void BindList() { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT CustomerId, Country FROM Customers&#34;, con)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { da.Fill(dt); ddlCountry.DataTextField = &#34;Country&#34;; ddlCountry.DataValueField = &#34;CustomerId&#34;; ddlCountry.DataSource = dt; ddlCountry.DataBind(); ddlCountry.Items.Insert(0, new ListItem(&#34;--Select Items--&#34;, &#34;0&#34;)); } } } } } protected void Delete(object sender, EventArgs e) { string confirmValue = Request.Form[&#34;confirm_value&#34;]; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;DELETE FROM Customers WHERE Country = @Country&#34;, con)) { cmd.Parameters.AddWithValue(&#34;@Country&#34;, ddlCountry.SelectedItem.Text); if (confirmValue == &#34;Yes&#34;) { this.Page.ClientScript.RegisterStartupScript(this.GetType(), &#34;alert&#34;, &#34;alert(&#39;You clicked YES!&#39;)&#34;, true); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } else { this.Page.ClientScript.RegisterStartupScript(this.GetType(), &#34;alert&#34;, &#34;alert(&#39;You clicked NO!&#39;)&#34;, true); } this.BindList(); } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT CustomerId, Country FROM Customers&#34;, con) Using da As SqlDataAdapter = New SqlDataAdapter(cmd) Using dt As DataTable = New DataTable() da.Fill(dt) ddlCountry.DataTextField = &#34;Country&#34; ddlCountry.DataValueField = &#34;CustomerId&#34; ddlCountry.DataSource = dt ddlCountry.DataBind() ddlCountry.Items.Insert(0, New ListItem(&#34;--Select Items--&#34;, &#34;0&#34;)) 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(&#34;confirm_value&#34;) Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As SqlConnection = New SqlConnection(constr) Using cmd As SqlCommand = New SqlCommand(&#34;DELETE FROM Customers WHERE Country = @Country&#34;, con) cmd.Parameters.AddWithValue(&#34;@Country&#34;, ddlCountry.SelectedItem.Text) If confirmValue = &#34;Yes&#34; Then Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), &#34;alert&#34;, &#34;alert(&#39;You clicked YES!&#39;)&#34;, True) con.Open() cmd.ExecuteNonQuery() con.Close() Else Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), &#34;alert&#34;, &#34;alert(&#39;You clicked NO!&#39;)&#34;, True) End If Me.BindList() End Using End Using End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/NvaKtCf.gif" alt="" width="336" height="254" /></p>https://www.aspforums.net:443/Threads/253690/Execute-code-based-on-Server-Side-Yes-No-Confirmation-Box-on-Button-Click-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/253690/Execute-code-based-on-Server-Side-Yes-No-Confirmation-Box-on-Button-Click-using-C-and-VBNet-in-ASPNet/Wed, 05 Dec 2018 23:26:20 GMT