Coolite 学习收集之AjaxMethod
AjaxMethod
Coolite Toolkit所提供的AjaxMethod功能和ASP.NET AJAX的PageMethod是一样的,使用非常简单,详细可参考下面的简单示例:
[AjaxMethod]
public string PageMethod()
{
return "调用了页面后置方法:PageMethod()";
}
<ext:ScriptManager ID="ScriptManager1" runat="server"></ext:ScriptManager>
<ext:Button ID="Button1" runat="server" Text="Submit">
<Listeners>
<Click Handler="RequestMethod()"/>
</Listeners>
</ext:Button>
<script type="text/javascript">
function RequestMethod()
{
Coolite.AjaxMethods.PageMethod({
success: function(result) {
Ext.Msg.alert('返回提示', result);
}
});
}
</script>
通过Coolite Toolkit提供的Coolie.AjaxMethods.方法名直接调用后置方法。相互之间传递数据(简单文本串,对象,JSON)支持多种格式,这点和ASP.NET AJAX是一样的。除此之外,Coolite Toolkit还提供了更强大的页面方法调用功能,就是他可以调用母版页(MasterPage)和用户自
[AjaxMethodProxyID(IDMode = AjaxMethodProxyIDMode.None)]
public partial class MyMaster : System.Web.UI.MasterPage
{
[AjaxMethod]
public string PageMethod()
{
return "调用了母版页的后置方法:PageMethod()";
}
}
前台页面调用:
<%@ Page Title="" Language="C#" MasterPageFile="~/MyMaster.Master" AutoEventWireup="true"
CodeBehind="MasterDemo.aspx.cs" Inherits="WebApplication1.MasterDemo"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<ext:ScriptManager ID="ScriptManager1" runat="server">
</ext:ScriptManager>
<ext:Button ID="Button1" runat="server" Text="Submit">
<Listeners>
<Click Handler="RequestMethod()"/>
</Listeners>
</ext:Button>
<script type="text/javascript">
function RequestMethod() {
Coolite.AjaxMethods.PageMethod({
success: function(result) {
Ext.Msg.alert('返回提示', result);
}
});
}
</script>
</asp:Content>
如果是调用用户自定义控件里的方法,方式和母版页相差不大,不同的是UserControl需要设置别名才能成功调用。如下示例:
[AjaxMethodProxyID(IDMode = AjaxMethodProxyIDMode.Alias, Alias ="UC")]
public partial class TimeControl : System.Web.UI.UserControl
{
[AjaxMethod]
public
string PageMethod()
{
return DateTime.Now.ToString();
}
}
客户端使用别名调用如下:
<uc1:TimeControl ID="TimeControl1" runat="server"/>
<ext:Button ID="btnUserControl" runat="server" Text="调用UserControl的方法">
<Listeners>
<Click Handler="Coolite.AjaxMethods.UC.PageMethod(
{
success:function(result)
{
Ext.Msg.alert(result);
}
});"/>
</Listeners>
</ext:Button>
注:以上内容收集于Coolite中文社区:http://www.coolite.net.cn/