Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

Introduction 

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.

FirstPage.aspx
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)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(SecondPage));
        }
    }
}

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

FirstPage.aspx
 <%@ 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">

    .style1
    {
        width: 100%;
    }
    .style2
    {
        height: 41px;
    }
    </style>
    <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);
        }
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <table class="style1">
        <tr>
            <td align="right">
            First Number</td>
            <td>
                <asp:TextBox ID="txtF1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td align="right">
            Second Number</td>
            <td>
                <asp:TextBox ID="txtF2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td colspan="2" class="style2">
            &nbsp;<center>
                    <asp:Button ID="Button1" runat="server" Text="Add" Height="27px" 
                    Width="59px" OnClientClick="AddNumbers();" />
                &nbsp;&nbsp;
                    <asp:Label ID="lblResult" runat="server"></asp:Label>
                </center>
            </td>
        </tr>
    </table>
</asp:Content>

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

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

    }
    [Ajax.AjaxMethod(HttpSessionStateRequirement.Read)]
    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:

Web.config
<system.web>
    <httpHandlers>
      <add verb="POST,GET" path="ajax/*.ashx" 
        type="Ajax.PageHandlerFactory, Ajax"/>
    </httpHandlers>

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.

License

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

Share

About the Author

Shivarajbk
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.

Comments and Discussions

 
QuestionWhy to call other page method? PinmemberMehul M Thakkar23-Dec-13 19:34 
GeneralMy vote of 2 PinmemberHiteshforu200726-Jun-13 3:06 
QuestionBad idea........... -100 vote PinmemberTridip Bhattacharjee19-Jun-13 20:52 
QuestionRe: Bad idea........... -100 vote PinmemberShivarajbk20-Jun-13 18:13 
AnswerRe: Bad idea........... -100 vote PinmemberTridip Bhattacharjee20-Jun-13 20:57 
GeneralRe: Bad idea........... -100 vote PinmemberShivarajbk20-Jun-13 23:49 
GeneralRe: Bad idea........... -100 vote PinmemberTridip Bhattacharjee23-Jun-13 20:48 
QuestionRe: Bad idea........... -100 vote PinmemberMehul M Thakkar23-Dec-13 19:42 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 19 Jun 2013
Article Copyright 2013 by Shivarajbk
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid