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

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

, 26 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Use AjaxManager to Generate JavaScript that calls the Server-Side Method

The original: http://zoyobar.blogspot.com/2011/12/use-ajaxmanager-to-generate-javascript.html

Introduction/Catalog

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

http://zoyobar.blogspot.com/2011/12/use-ajaxmanager-to-generate-javascript.html

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:

<%@ 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:

<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:

<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:

<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:

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

Direct Call

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

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

Call By Async Property

JQueryElement controls can invoke the generated methods by Async property:

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

<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 due to 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)

Share

About the Author

zoyobar

United States United States
No Biography provided

Comments and Discussions

 
GeneralNicely writtena & useful Pinmembersudhansu_k12319-Dec-11 20:50 
GeneralRe: Nicely writtena & useful Pinmemberzoyobar26-Dec-11 13:51 
GeneralMy vote of 4 PinmemberMonjurul Habib12-Dec-11 9:55 
GeneralRe: My vote of 4 Pinmemberzoyobar26-Dec-11 13:50 

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
Web03 | 2.8.140916.1 | Last Updated 26 Dec 2011
Article Copyright 2011 by zoyobar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid