The original post can be found here.
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:
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>
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.
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" />
THe above example has shown a direct calling, and it's the same with calling a normal JavaScript method:
<script>
$(function () {
add3(1);
});
</script>
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
.
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
.
comment