Click here to Skip to main content
13,053,971 members (58,401 online)
Click here to Skip to main content
Add your own
alternative version


2 bookmarked
Posted 19 Jun 2013

Reuse of server methods using AJAX DLL

, 19 Jun 2013
Rate this:
Please Sign up or sign in to vote.
This atricle explains how to access methods from another server page.


This articles manly explains how to use the AJAX DLL and reuse methods from another server page. If we have a method in another server page and want to access it from your page, by using AJAX DLL we can call the server side methods from JavaScript, and we can pass the method parameter values from JavaScript. 

An advantage of coding with the AJAX DLL is if you need to call a method from another server page you can easily do this by registering in the page load of your main server page.

Using the code

Let's create two pages: FirstPage.aspx and SecondPage.aspx. In FirstPage.aspx I have only a design part for adding two numbers, and I will create a method to perform the addition in SecondPage.cs. Create a simple design in your FirstPage.aspx for adding two numbers.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ajax;
namespace UserControlUsingAjax
    public partial class FirstPage : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)

With this registration we can access the server methods of SecondPage.aspx.cs through JavaScript.

 <%@ Page Title="" Language="C#" 

  MasterPageFile="~/Site.Master" AutoEventWireup="true" 

  CodeBehind="FirstPage.aspx.cs" Inherits="UserControlUsingAjax.FirstPage" %>
<asp:Content ID="Content1" 

  ContentPlaceHolderID="HeadContent" runat="server">
    <style type="text/css">

        width: 100%;
        height: 41px;
    <script type="text/javascript">
        function AddNumbers() {
            var fnumber = document.getElementById('<%=txtF1.ClientID%>').value;
            var Snumber = document.getElementById('<%=txtF2.ClientID%>').value;
            var s = SecondPage.AddTwoNumbers(fnumber, Snumber);
            alert('Result is ' + s.value);
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table class="style1">
            <td align="right">
            First Number</td>
                <asp:TextBox ID="txtF1" runat="server"></asp:TextBox>
            <td align="right">
            Second Number</td>
                <asp:TextBox ID="txtF2" runat="server"></asp:TextBox>
            <td colspan="2" class="style2">
                    <asp:Button ID="Button1" runat="server" Text="Add" Height="27px" 

                    Width="59px" OnClientClick="AddNumbers();" />
                    <asp:Label ID="lblResult" runat="server"></asp:Label>

In FirstPage.aspx page load, we registered SecondPage so now we can access the methods of SecondPage using JavaScript:

var s = SecondPage.AddTwoNumbers(fnumber, Snumber);

Now in the next step we need create a method AddTwoNumbers in SecondPage

public partial class SecondPage : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    public string AddTwoNumbers(string a, string b)

        return (Convert.ToInt32(a) + Convert.ToInt32(b)).ToString();

And we need to register AJAX in web.Config in system.web:

      <add verb="POST,GET" path="ajax/*.ashx" 

        type="Ajax.PageHandlerFactory, Ajax"/>

The drawback of using the AJAX DLL is that we cannot access our server control in our method directly but we can access server control values through JavaScript and we can pass to AJAX methods which are there in the server page.


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


About the Author

Software Developer TWB BANGALORE
India India
I Started my Programming career with C#.Currently using C#, ASP.NET, WCF, AJAX, & ASP.NET MVC to create Information Systems.

My interests involves Programming, C# is the best programming language and I love working with C# and other Microsoft Technologies.

You may also be interested in...


Comments and Discussions

QuestionWhy to call other page method? Pin
Mehul M Thakkar23-Dec-13 19:34
memberMehul M Thakkar23-Dec-13 19:34 
GeneralMy vote of 2 Pin
Hiteshforu200726-Jun-13 3:06
memberHiteshforu200726-Jun-13 3:06 
QuestionBad idea........... -100 vote Pin
Tridip Bhattacharjee19-Jun-13 20:52
memberTridip Bhattacharjee19-Jun-13 20:52 
QuestionRe: Bad idea........... -100 vote Pin
Shivarajbk20-Jun-13 18:13
memberShivarajbk20-Jun-13 18:13 
AnswerRe: Bad idea........... -100 vote Pin
Tridip Bhattacharjee20-Jun-13 20:57
memberTridip Bhattacharjee20-Jun-13 20:57 
GeneralRe: Bad idea........... -100 vote Pin
Shivarajbk20-Jun-13 23:49
memberShivarajbk20-Jun-13 23:49 
GeneralRe: Bad idea........... -100 vote Pin
Tridip Bhattacharjee23-Jun-13 20:48
memberTridip Bhattacharjee23-Jun-13 20:48 
QuestionRe: Bad idea........... -100 vote Pin
Mehul M Thakkar23-Dec-13 19:42
memberMehul M Thakkar23-Dec-13 19:42 

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.170713.1 | Last Updated 19 Jun 2013
Article Copyright 2013 by Shivarajbk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid