Click here to Skip to main content
12,896,108 members (50,495 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked
Posted 4 Nov 2013

Access Server Side (webservice) from JavaScript in Master Page

, 5 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip describes the steps to access server side (webservice) from JavaScript in master page.


This tip describes the steps to access server side (webservice) from JavaScript in master page.

Using the Code

This code can be helpful for situations when you need to access server side from master page avoiding full page postback.

You need to follow the steps given below:

  1. Add webservice (asmx file) that contains the server side function you want to access from JavaScript:
    public void AtServerSide(int a , string b)    
     int i = 0;    
  2. To access the Session variable in the webservice, you need to add the attribute EnableSession = true:
    [WebMethod(EnableSession = true)]
    public void AtServerSide(int a , string b)    
     Session["CountCalledFromJS"] = Convert.ToInt32(Session["CountCalledFromJS"]) + 1;  
  3. Uncomment the line to allow webservice to be accessed from JavaScript:
  4. Add master page and add reference to your webservice created above (add code in masterpage.master after form tag):
    <asp:ScriptManager ID="ScriptManager1" 
    runat="server" EnablePageMethods="true">
            <asp:ServiceReference Path="../webservice.asmx" />
  5. Add HTML markup to master page to call to server:
     <h1>Ajax Call from master page to asmx through javascript</h1>
    <label title="Click to access server from javascript" 
    onclick="AccessServerSide();">Click to access server from javascript</label> 
  6. Add JavaScript in masterpage / separate file to call the webservice as normal function call. Here my webservice class name is "Webservice". You need to replace the text with your webservice class name.
    <script type="text/javascript">
        function AccessServerSide() {
            WebService.AtServerSide(1, 'Hello');
  7. Add normal aspx page that uses master page and run the solution.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Muhammad Waqas Iqbal
Software Developer (Senior) Freelancer
Pakistan Pakistan
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionUsing this sample at sharePoint 2013 Pin
mal315416-Jun-14 21:50
membermal315416-Jun-14 21:50 
AnswerRe: Using this sample at sharePoint 2013 Pin
m.waqasiqbal17-Jun-14 15:21
memberm.waqasiqbal17-Jun-14 15:21 
QuestionEnable Page Methods Pin
prodigy_dave4-Nov-13 3:33
memberprodigy_dave4-Nov-13 3:33 
AnswerRe: Enable Page Methods Pin
m.waqasiqbal4-Nov-13 6:19
memberm.waqasiqbal4-Nov-13 6:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 5 Nov 2013
Article Copyright 2013 by Muhammad Waqas Iqbal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid