Ext.NET 基础学习笔记04 (DirectMethod方法详解)
1.使用DirectMethods在JS中调用C#方法
<script type="text/javascript">
var fnUpdate = function () {
App.direct.AddToServerTime(3);
}
</script>
<ext:ResourceManager runat="server"></ext:ResourceManager>
<form id="form1" runat="server">
<ext:Window runat="server" ID="win1" Width="300" Height="200" Title="DirectMethod">
<Items>
<ext:Label runat="server" ID="myLabel" />
</Items>
<Buttons>
<ext:Button runat="server" ID="btnUpdate" Text="更新" Handler="fnUpdate()"></ext:Button>
</Buttons>
</ext:Window>
</form>
<ext:Window runat="server" ID="win1" Width="300" Height="200" Title="DirectMethod">
<Items>
<ext:Label runat="server" ID="myLabel" />
</Items>
<Buttons>
<ext:Button runat="server" ID="btnUpdate" Text="更新" Handler="fnUpdate()"></ext:Button>
</Buttons>
</ext:Window>
</form>
后台C#代码:
[DirectMethod]
public void AddToServerTime(int hours)
{
var date = DateTime.Now.Add(new TimeSpan(hours, 0, 0));
this.myLabel.Text = date.ToString();
}
public void AddToServerTime(int hours)
{
var date = DateTime.Now.Add(new TimeSpan(hours, 0, 0));
this.myLabel.Text = date.ToString();
}
2.设置DirectMethods的默认命名空间
DirectMethod比DirectEvents更加灵活,通过DirectMethods在JS中通过异步调用的方式执行服务器端的方法.
2.1 在Web.config文件的<extnet>中添加directMethodNamespace(对所有页面有效)
<extnet theme="Triton" directMethodNamespace="MyApp.DirectMethod" />
调用时:
<script type="text/javascript">
var fnUpdate = function () {
App.direct.AddToServerTime(3);
}
</script>
var fnUpdate = function () {
App.direct.AddToServerTime(3);
}
</script>
2.2 在<ext:ResourceManager>中添加DirectMethodNamespace属性(只对当前页面有效,覆盖全局的设置)
<ext:ResourceManager runat="server" DirectMethodNamespace="MyApp.DirectMethod"></ext:ResourceManager>
2.3 使用DirectMethod特性来更改(针对某一个方法,覆盖页面和全局设定)
[DirectMethod(Namespace="MyApp.Method")]
public void AddToServerTime(int hours)
{
var date = DateTime.Now.Add(new TimeSpan(hours, 0, 0));
this.myLabel.Text = date.ToString();
}
public void AddToServerTime(int hours)
{
var date = DateTime.Now.Add(new TimeSpan(hours, 0, 0));
this.myLabel.Text = date.ToString();
}
3.回调方法
var fnUpdate = function () {
MyApp.Method.AddToServerTime(3, {
success: onSuccess,
failure: onFailure,
eventMask: { showMask: true } //显示一个遮罩层
});
}
MyApp.Method.AddToServerTime(3, {
success: onSuccess,
failure: onFailure,
eventMask: { showMask: true } //显示一个遮罩层
});
}
function onSuccess() {
alert("成功");
}
alert("成功");
}
function onFailure() {
alert("失败");
}
alert("失败");
}
4.以同步执行服务器端方法
[DirectMethod(Namespace = "MyApp.DirectMethods", Async = false)]
public void AddToServerTime(int hours)
{
string text = this.btnUpdate.Text;
var date = DateTime.Now.Add(new TimeSpan(hours, 0, 0));
this.myLabel.Text = text + date.ToString();
}
public void AddToServerTime(int hours)
{
string text = this.btnUpdate.Text;
var date = DateTime.Now.Add(new TimeSpan(hours, 0, 0));
this.myLabel.Text = text + date.ToString();
}
5.调用静态方法-提高性能
在静态方法中我们无法访问页面的Form表单元素,不能直接访问Request,Response(可以通过HttpContext.Current来访问)
使用静态方法的好处:在请求的时候Ext.Net不会讲页面数据提交给服务器,大大减少数据传输和服务器执行的时间
var fnUpdate = function () {
MyApp.Method.AddToServerTime(3, {
success: onSuccess,
failure: onFailure,
eventMask: { showMask: true }
});
}
MyApp.Method.AddToServerTime(3, {
success: onSuccess,
failure: onFailure,
eventMask: { showMask: true }
});
}
function onSuccess(result) {
App.myLabel.setText(result);
}
App.myLabel.setText(result);
}
function onFailure() {
alert("失败");
}
alert("失败");
}
后台C#代码:
[DirectMethod(Namespace = "MyApp.Method")]
public static string AddToServerTime(int hours)
{
var date = DateTime.Now.Add(new TimeSpan(hours, 0, 0));
return date.ToString();
}
public static string AddToServerTime(int hours)
{
var date = DateTime.Now.Add(new TimeSpan(hours, 0, 0));
return date.ToString();
}
6.使用Ext.net.DirectMethod.request调用WebService
var showRelativeServerTime = function () {
Ext.net.DirectMethod.request({
url: "WebServices/ExtNetService.asmx/HelloWorld",
cleanRequest: true,
params: {
name: "Jack!"
},
success: function (result) {
alert(Ext.DomQuery.selectValue("string", result, ""));
}
});
}
Ext.net.DirectMethod.request({
url: "WebServices/ExtNetService.asmx/HelloWorld",
cleanRequest: true,
params: {
name: "Jack!"
},
success: function (result) {
alert(Ext.DomQuery.selectValue("string", result, ""));
}
});
}
Webservices代码:
[WebMethod]
public string HelloWorld(string name)
{
return "Hello : " + name;
}
public string HelloWorld(string name)
{
return "Hello : " + name;
}