Calling server side method by using Java Script.

We can call the server side function in several ways. Here we will discuss on calling web method by using Page method object on java script. From below example, I am calling the server side method when user closes the ASP.NET web application window.

Steps to call the server side method by using Java Script.
1) Open Visual studio and select Web Service from VB.Net templates.
2) Create one Web Method by using your own implementation.
3) Drag the AJAX Script Manger and set the property "EnablePageMethods="true"";
4) Call the server side method by using Page Method object. Ex. PageMethods.deleteMethod(14);

HTML Part:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication5._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />


<script type="text/javascript">
window.onbeforeunload = function(e)
try {
e = e || window.event;

alert("Window is closing...");
catch (ex)

Code Behind Code in VB.NET:

Imports System.Data.OleDb

Partial Public Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
Public Shared Function deleteMethod(ByVal custid As String) As String
Dim conn As OleDbConnection = Nothing

Dim connection As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
conn = New OleDbConnection(connection)

Dim cmd As New Data.OleDb.OleDbCommand("Delete From table1", conn)
Dim da As New Data.OleDb.OleDbDataAdapter(cmd)
Dim ds As New Data.DataSet("table1")

da.Fill(ds, "table1")
Catch ex As Exception
Dim ans As String
ans = MsgBox(ex.ToString)
End Try
Return String.Empty
End Function

End Class


No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: