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.Call Server Side function from JavaScript on JavaScript Confirm Box Yes Button Click in ASP.Net<p>Hi&nbsp;srihitha,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <p>I have made use of the following table Customers with the schema as follows.</p> <p><img src="https://www.aspsnippets.com/Handlers/DownloadFile.ashx?File=f18ac914-bc9b-437a-88e2-bd640ce05282.png" alt="" width="384" height="137" /></p> <p>I have already inserted few records in the table.</p> <p>You can download the database table SQL by clicking the download link below.</p> <p><a href="https://www.aspsnippets.com/DownloadFile.aspx?File=Customers_Table_AutoIncrement.sql">Download SQL file</a></p> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:ScriptManager runat=&#34;server&#34; EnablePageMethods=&#34;true&#34; /&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;GridView1&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&#34;Select&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&#34;btnDelete&#34; Text=&#34;Delete&#34; CommandArgument=&#39;&lt;%#Eval(&#34;CustomerId&#34;)%&gt;&#39; CommandName=&#34;rowDelete&#34; runat=&#34;server&#34; OnClientClick=&#34;if(!SubUnitDeleteErrorWarning_SelectedItem(this)) return false;&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;Id&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;script type=&#34;text/javascript&#34;&gt; function SubUnitDeleteErrorWarning_SelectedItem(lnk) { var row = lnk.parentNode.parentNode; var rowIndex = row.rowIndex - 1; var country = row.cells[3].innerHTML; PageMethods.GetSubUnitDeleteWarningByID(country, onSuccess, onError); function onSuccess(result) { if (result &gt; 0) { if (confirm(&#34;Do you want to delete this subunit so &#34; + result + &#34; travelers will be affected do you want to continue still?&#34;)) { PageMethods.DeleteSubUnitByID(country, function () { alert(result + &#34; Travelers deleted.&#34;); window.location.reload(); }, onError); } } else { alert(&#34;No Travelers will be affected if you delete this subunit.&#34;) return false; } } function onError(err) { alert(err); } } &lt;/script&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.Services;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient Imports System.Web.Services</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { GridView1.DataSource = GetData(); GridView1.DataBind(); } } private static DataTable GetData() { string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; string query = &#34;SELECT * FROM Customers&#34;; SqlCommand cmd = new SqlCommand(query); using (SqlConnection con = new SqlConnection(conString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); return dt; } } } } [WebMethod] public static string GetSubUnitDeleteWarningByID(string country) { return GetData().Select(&#34;Country=&#39;&#34; + country + &#34;&#39;&#34;).Length.ToString(); } [WebMethod] public static string DeleteSubUnitByID(string country) { string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; string query = &#34;DELETE FROM Customers WHERE Country = @Country&#34;; SqlConnection con = new SqlConnection(conString); SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue(&#34;@Country&#34;, country); con.Open(); int count = cmd.ExecuteNonQuery(); con.Close(); return count &gt; 0 ? &#34;1&#34; : &#34;0&#34;; }</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 GridView1.DataSource = GetData() GridView1.DataBind() End If End Sub Private Shared Function GetData() As DataTable Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim query As String = &#34;SELECT * FROM Customers&#34; Dim cmd As SqlCommand = New SqlCommand(query) Using con As SqlConnection = New SqlConnection(conString) Using sda As SqlDataAdapter = New SqlDataAdapter() cmd.Connection = con sda.SelectCommand = cmd Using dt As DataTable = New DataTable() sda.Fill(dt) Return dt End Using End Using End Using End Function &lt;WebMethod()&gt; Public Shared Function GetSubUnitDeleteWarningByID(ByVal country As String) As String Return GetData().Select(&#34;Country=&#39;&#34; &amp; country &amp; &#34;&#39;&#34;).Length.ToString() End Function &lt;WebMethod()&gt; Public Shared Function DeleteSubUnitByID(ByVal country As String) As String Dim conString As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim query As String = &#34;DELETE FROM Customers WHERE Country = @Country&#34; Dim con As SqlConnection = New SqlConnection(conString) Dim cmd As SqlCommand = New SqlCommand(query, con) cmd.Parameters.AddWithValue(&#34;@Country&#34;, country) con.Open() Dim count As Integer = cmd.ExecuteNonQuery() con.Close() Return If(count &gt; 0, &#34;1&#34;, &#34;0&#34;) End Function</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/nHuHbPg.gif" alt="" width="441" height="264" /></span></strong></p>https://www.aspforums.net:443/Threads/622586/Call-Server-Side-function-from-JavaScript-on-JavaScript-Confirm-Box-Yes-Button-Click-in-ASPNet/https://www.aspforums.net:443/Threads/622586/Call-Server-Side-function-from-JavaScript-on-JavaScript-Confirm-Box-Yes-Button-Click-in-ASPNet/Tue, 06 Nov 2018 01:58:36 GMT