There is no physical existence of WebMethod after the page is rendered. So, jQuery Ajax call is not possible to a WebMethod in Master Page.
Your WebMethod code cannot reside in the code behind for your Master Page.
It is easier to include a WebService or WCF service in your project.
Refer the below sample code for your reference if you want to call WebMethod by Ajax call using WebService on MasterPage image button click event.
MasterPage.master
HTML
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("[id*=ImageButton1]").click(function (event) {
var lang = $("[id*=dlLanguage] option:selected").val();
$.ajax({
type: "POST",
url: "WebService1.asmx/CallMethod",
data: "{lang:'" + lang + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.responseText);
}
});
});
});
function OnSuccess(response) {
var popup;
popup = window.open(response.d, "customWindow", "width=500, height=300, top=130, left = 5000, AddressBar=No,status=no,directories=no,menubar=no,toolbar=no,scrollbars=no, location=no,resizable=false,titlebar=no");
popup.focus();
}
</script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server" onsubmit="return false;">
<div>
<asp:ImageButton runat="server" ID="ImageButton1" ImageUrl="~/Images/LinkImage.png"
Width="20" Height="20" />
<br />
<asp:DropDownList ID="dlLanguage" runat="server">
<asp:ListItem Text="English" Value="en"></asp:ListItem>
<asp:ListItem Text="French" Value="fr"></asp:ListItem>
</asp:DropDownList>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
CS.aspx
HTML
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="CS.aspx.cs" Inherits="CS" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
This is CS Page
</asp:Content>
WebService1.asmx
<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService1.cs" Class="WebService1" %>
WebService1.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string CallMethod(string lang)
{
var re = HttpContext.Current.Request.UrlReferrer.AbsolutePath;
string valueId = "";
if (re == "w1/")
{
valueId = System.Configuration.ConfigurationManager.AppSettings["h"].ToString().Split('=')[1];
}
else if (re == "w2/")
{
valueId = System.Configuration.ConfigurationManager.AppSettings["f"].ToString().Split('=')[1];
}
else if (re == "w3/")
{
valueId = System.Configuration.ConfigurationManager.AppSettings["l"].ToString().Split('=')[1];
}
var Url = System.Configuration.ConfigurationManager.AppSettings["_URL"];
string queryString = (Url + "?parm=" + valueId + "&PostNm=" + lang + "");
var screenWidth = HttpContext.Current.Request.Browser.ScreenPixelsWidth;
return queryString;
}
}
Screenshot
