Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
26 Dec 2011 1  
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:

<%@ Register Assembly="zoyobar.shared.panzer.JQueryElement"
 Namespace="zoyobar.shared.panzer.ui.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

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 property FillAsync will call the method fill, so the fill method is implicitly added pageIndex and pagesize.

Related Content

comment

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here