Click here to Skip to main content
15,885,365 members
Articles / Web Development / ASP.NET
Tip/Trick

Access Server Side (webservice) from JavaScript in Master Page

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
5 Nov 2013CPOL 16.3K   7   4
This tip describes the steps to access server side (webservice) from JavaScript in master page.

Introduction

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:
    C#
    [WebMethod]    
    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:
    C#
    [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:
    C#
    [System.Web.Script.Services.ScriptService]    
  4. Add master page and add reference to your webservice created above (add code in masterpage.master after form tag):
    ASP.NET
    <asp:ScriptManager ID="ScriptManager1" 
    runat="server" EnablePageMethods="true">
        <Services>
            <asp:ServiceReference Path="../webservice.asmx" />
        </Services>
    </asp:ScriptManager> 
  5. Add HTML markup to master page to call to server:
    ASP.NET
     <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.
    JavaScript
    <script type="text/javascript">
        function AccessServerSide() {
            WebService.AtServerSide(1, 'Hello');
        }
    </script> 
  7. Add normal aspx page that uses master page and run the solution.

License

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


Written By
Software Developer (Senior) Freelancer
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUsing this sample at sharePoint 2013 Pin
Simin Maleki Shamasbi16-Jun-14 21:50
Simin Maleki Shamasbi16-Jun-14 21:50 
AnswerRe: Using this sample at sharePoint 2013 Pin
Muhammad Waqas Iqbal17-Jun-14 15:21
Muhammad Waqas Iqbal17-Jun-14 15:21 
QuestionEnable Page Methods Pin
prodigy_dave4-Nov-13 3:33
prodigy_dave4-Nov-13 3:33 
AnswerRe: Enable Page Methods Pin
Muhammad Waqas Iqbal4-Nov-13 6:19
Muhammad Waqas Iqbal4-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.