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

Reuse of server methods using AJAX DLL

Rate me:
Please Sign up or sign in to vote.
4.57/5 (3 votes)
19 Jun 2013CPOL1 min read 15.1K   2   8
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
C#
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
ASP.NET
 <%@ 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:

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

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

SecondPage.aspx.cs
C#
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
XML
<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)


Written By
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? Pin
Mehul M Thakkar23-Dec-13 19:34
Mehul M Thakkar23-Dec-13 19:34 
GeneralMy vote of 2 Pin
Hiteshforu200726-Jun-13 3:06
Hiteshforu200726-Jun-13 3:06 
Tedious way of doing stuff when more efficient and easy tools are available.
QuestionBad idea........... -100 vote Pin
Tridip Bhattacharjee19-Jun-13 20:52
professionalTridip Bhattacharjee19-Jun-13 20:52 
QuestionRe: Bad idea........... -100 vote Pin
Shivarajbk20-Jun-13 18:13
Shivarajbk20-Jun-13 18:13 
AnswerRe: Bad idea........... -100 vote Pin
Tridip Bhattacharjee20-Jun-13 20:57
professionalTridip Bhattacharjee20-Jun-13 20:57 
GeneralRe: Bad idea........... -100 vote Pin
Shivarajbk20-Jun-13 23:49
Shivarajbk20-Jun-13 23:49 
GeneralRe: Bad idea........... -100 vote Pin
Tridip Bhattacharjee23-Jun-13 20:48
professionalTridip Bhattacharjee23-Jun-13 20:48 
QuestionRe: Bad idea........... -100 vote Pin
Mehul M Thakkar23-Dec-13 19:42
Mehul 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.