Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / Javascript

Use AjaxManager to Generate JavaScript that calls the Server-Side Method

Rate me:
Please Sign up or sign in to vote.
4.25/5 (3 votes)
26 Dec 2011CPOL2 min read 21.4K   6   4
Use AjaxManager to Generate JavaScript that calls the Server-Side Method

The original post can be found here.

Introduction/Catalog

Due to limited time, synchronization cannot be guaranteed in more than one blog article. At the following address, you can view up-to-date content, hope you understand:

Please download the sample code at the section JQueryElement demo download of Download JQueryElement, the directory is /ajaxmanager/Default.aspx.

This article explains how to use AjaxManager to generate the JavaScript that calls the server-side method, and how to call these methods:

Prepare

Be sure that you have got the latest version of JQueryElement at the section JQueryElement.dll download of Download JQueryElement.

Use the following statements to reference namespace:

ASP.NET
<%@ Register Assembly="zoyobar.shared.panzer.JQueryElement"
 Namespace="zoyobar.shared.panzer.ui.jqueryui"
 TagPrefix="je" %>
<%@ Register Assembly="zoyobar.shared.panzer.JQueryElement"
 Namespace="zoyobar.shared.panzer.web.jqueryui"
 TagPrefix="je" %>  

In addition to the namespace, you need to reference the jQueryUI scripts:

JavaScript
<script type="text/javascript"
 src="[script path]/jquery-<version>.min.js"></script>  

Creating JavaScript

In the page, we add a AjaxManager control to create a JavaScript method that calls the server-side method:

JavaScript
<je:AjaxManager ID="manager" runat="server">
 <AjaxList>
  <je:AjaxSetting
   ClientFunction="<javascript method name>"
   ClientParameter="<javascript parameter, ??: name, age>"
   Url="<server-side method url>" MethodName="<server-side method name>"
   Success="<javascript method invoked on success>"
   Error="<javascript method invoked when error>"
   Complete="<javascript method invoked when completed>"
   ...
   >
   <ParameterList>
    <parameter>
   </ParameterList>
  </je:AjaxSetting>
 </AjaxList>
</je:AjaxManager>

<je:AjaxManager ID="manager" runat="server">
 <AjaxList>
  <je:AjaxSetting ClientFunction="add" Url="handler.ashx" Success="
  function(data){
   $('#result').text(-:data.result);
  }
  ">
   <ParameterList>
    <je:Parameter Name="c" Type="Expression" Value="'add'" />
    <je:Parameter Name="num1" Type="Selector"
     Value="'#num1'" DataType="Number" />
    <je:Parameter Name="num2" Type="Selector"
     Value="'#num2'" DataType="Number" />
   </ParameterList>
  </je:AjaxSetting>
 </AjaxList>
</je:AjaxManager>  

The above example generates a JavaScript method called add, the generic handler handler.ashx will be called in the method to return JSON data.

Code -:data will be replaced with data or data.d, for more, please refer to Return JSON In different .NET Version.

We can add Ajax request parameter through Parameter, please refer to Through Parameter Object Add Ajax Request Parameter for details.

Set Parameter List of the JavaScript Method

Through the property ClientParameter, you can set the parameter list of a JavaScript method:

JavaScript
<je:AjaxManager ID="manager" runat="server">
 <AjaxList>
  <je:AjaxSetting ClientFunction="add3" ClientParameter="othernum"
  Url="handler.ashx"
  ... >
   <ParameterList>

    <je:Parameter Name="num3" Type="Expression" Value="othernum" />

   </ParameterList>
  </je:AjaxSetting>
 </AjaxList>
</je:AjaxManager>  

In the example above, by adding the parameter othernum for the method add3, the value of parameter othernum is passed to the server side as num3. add3 is invoked like this:

JavaScript
<input type="button" onclick="javascript:add3(1);" value="plus 1" />  

Direct Call

THe above example has shown a direct calling, and it's the same with calling a normal JavaScript method:

JavaScript
<script>
 $(function () {
  add3(1);
 });
</script>  

Call By Async Property

JQueryElement controls can invoke the generated methods by Async property:

JavaScript
<je:Button ID="cmdSub" runat="server" IsVariable="true"
 Label="sub" Disabled="true"
 ClickAsync-AjaxManagerID="manager"
 ClickAsync-ClientFunction="sub">
</je:Button>  

Using AjaxManagerID to specify the AjaxManager which contains the JavaScript method that you want to call, specify the JavaScript method name by ClientFunction.

Implicit Parameters

Part of the JQueryElement control will add the parameters to AjaxManager, such as the Repeater can add the pageindex, pagesize, etc.:

JavaScript
<je:Repeater ID="repeater" runat="server"
 FillAsync-AjaxManagerID="manager" FillAsync-ClientFunction="fill">
</je:Repeater>

<je:AjaxManager ID="manager" runat="server">
 <AjaxList>
  <je:AjaxSetting ClientFunction="fill" ClientParameter="othernum"
  Url="handler.ashx"
  ... >
   <ParameterList>
   </ParameterList>
  </je:AjaxSetting>
 </AjaxList>
</je:AjaxManager>  

The method fill does not add any Parameter, but property FillAsync will call the method fill, so the fill method is implicitly added pageIndex and pagesize.

Related Content

comment

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNicely writtena & useful Pin
sudhansu_k12319-Dec-11 20:50
sudhansu_k12319-Dec-11 20:50 
GeneralRe: Nicely writtena & useful Pin
zoyobar26-Dec-11 13:51
zoyobar26-Dec-11 13:51 
GeneralMy vote of 4 Pin
Monjurul Habib12-Dec-11 9:55
professionalMonjurul Habib12-Dec-11 9:55 
GeneralRe: My vote of 4 Pin
zoyobar26-Dec-11 13:50
zoyobar26-Dec-11 13:50 

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.