您的位置:首页 > 其它

Ext.net.DirectMethods

2012-12-10 21:31 197 查看
类别:Ext.Net来源:http://www.cnblogs.com/liuning8023/category/314933.html日期:2011.11.30阅读:173

DirectMethod提供了一种可以从客户端JavaScript代码调用服务器端.NET方法的功能。

用[DirectMethod]属性修饰服务器端的public或publicstatic方法,会向客户端JavaScript代码公开服务器端方法。

注意:服务器端的方法必须是public或publicstatic。

DirectMethod基础

下面代码演示DirectMethod一个简单的例子,更新<ext:Label>控件。

<scriptrunat="server">

[DirectMethod]

publicvoidSetTimeStamp()

{

this.Label1.Text=DateTime.Now.ToLongTimeString();

this.Label1.Element.Highlight();

}

</script>

<ext:ButtonID="Button1"runat="server"Text="ClickMe"Icon="Lightning">
<Listeners>

<ClickHandler="Ext.net.DirectMethods.SetTimeStamp();"/>

</Listeners>

</ext:Button>

<br/>

<ext:LabelID="Label1"runat="server"Format="ServerTime:{0}"Text='<%#DateTime.Now.ToLongTimeString()%>'/>


说明

1,在Button1客户端事件里,调用服务器端方法SetTimeStamp来更新Label1控件,并高亮显示。

2,另外,在Ext.Net,当第一次请求页面时(此时为回发),IsAjaxRequest为false,之后为true,因为之后的请求是Ajax请求。

从DirectMethod返回一个字符串

DirectMethod会返回任何类型的对象。这个对象被序列化成JSON。被系列化的这个对象作为result参数发送给在DirectMethod中配置的回调函数success。

<scriptrunat="server">

[DirectMethod]

publicstringGetTimeStamp()

{

returnDateTime.Now.ToLongTimeString();

}

</script>


<ext:Buttonrunat="server"Text="ClickMe"Icon="Lightning">

<Listeners>

<ClickHandler="

Ext.net.DirectMethods.GetTimeStamp({

success:function(result){

Ext.Msg.alert('ServerTime',result);

}

});"/>

</Listeners>

</ext:Button>


说明

在Button1客户端事件中,Ext.net.DirectMethods.GetTimeStamp(…)是在客户端调用服务器端的方法GetTimeStamp,success是Ext.net.DirectMethods配置的回调函数,也就是说,当服务器端方法成功返回时,客户端需要根据返回的值执行的操作。在本例中,如果服务器端方法GetTimeStamp()成功返回服务器端当前时间,则客户端弹出这个时间警告。

给DirectMethod传递多个参数

如果服务器端[DirectMethod]方法要求参数,那么也要客户端DirectMethod传递两个参数给它。

本例中,如果服务器端要求两个参数:sting和int,那么在客户端也要传递两个可靠的参数给服务器端的[DirectMethod]方法。

<scriptrunat="server">

[DirectMethod]

publicvoidLogCompanyInfo(stringname,intcount)

{

stringtemplate=string.Concat("{0}hasapproximately{1}employees.");

string[]employees=newstring[4]{"1-5","6-25","26-100","100+"};


this.Label3.Text=string.Format(template,name,employees[count]);

}

</script>


<ext:Buttonrunat="server"Text="Submit">

<Listeners>

<ClickHandler="Ext.net.DirectMethods.LogCompanyInfo('Ext.NET,Inc.',0);"/>

</Listeners>

</ext:Button>


调用DirectMethod静态方法,并返回一个字符串(SuperFast+BestPerformance)

当调用一个public服务端方法,默认情况下,在执行整个页面生命期时,这个方法可以访问页面上所有Web控件。

而带static的[DirectMethod]方法,不会执行页面生存期,并且不能访问页面Web控件。这减少了处理开销,优化了性能。

<scriptrunat="server">

[DirectMethod]

publicstaticstringGetTimeStamp4()

{

returnDateTime.Now.ToLongTimeString();

}

</script>


<ext:Buttonxrunat="server"Text="ClickMe"Icon="Lightning">

<Listeners>

<ClickHandler="

Ext.net.DirectMethods.GetTimeStamp4({

success:function(result){

Ext.Msg.alert('ServerTime',result);

}

});"/>

</Listeners>

</ext:Button>


说明

Button1客户端事件调用服务器端静态方法GetTimeStamp(),获得服务器端当前时间。

注意:服务器端静态方法GetTimeStamp()中不能访问页面中的Web控件。

从静态DirectMethod返回一个自定义对象

DirectMethod可以返回任何类型的对象。下面例子创建并返回一个Customer对象。

Customer对象被序列化成JSON,返回给客户端。在DirectMethod配置中,result参数就是从服务器端返回的Customer对象。

<scriptrunat="server">

//DefineCustomerClass

publicclassCustomer

{

publicintID{get;set;}

publicstringFirstName{get;set;}

publicstringLastName{get;set;}

publicstringCompany{get;set;}

publicCountryCountry{get;set;}

publicboolPremium{get;set;}

}


//DefineCountryClass

publicclassCountry

{

publicstringName{get;set;}

}


[DirectMethod]

publicstaticCustomerGetCustomer()

{

//GetyourCustomerdatafromsomewhere...

returnnewCustomer(){
ID=99,

FirstName="Peter",

LastName="Smith",

Company="CompanyX,LLC.",

Premium=true,

Country=newCountry{Name="Canada"}

};

}

</script>


<ext:Buttonrunat="server"Text="ClickMe"Icon="Lightning">

<Listeners>

<ClickHandler="

Ext.net.DirectMethods.GetCustomer({

success:function(customer){

vartemplate='ID:{0}{6}Name:{1}{2}{6}Company:{3}{6}Country:{4}{6}PremiumMember:{5}',

msg=String.format(template,

customer.ID,

customer.FirstName,

customer.LastName,

customer.Company,

customer.Country.Name,

customer.Premium,

'<br/><br/>');


Ext.Msg.alert('Customer',msg);

}

});"/>

</Listeners>

</ext:Button>


说明

1,定义两个类Customer和Country,Country聚合在Customer;

2,服务器端静态方法GetCustomer()创建Customer对象返回给客户端。

注意:客户端如何访问对象Customer。

禁用DirectMethodClientProxy的创建

当服务器端方法添加[DirectMethod]属性,默认情况下,将会在客户端的Ext.net.DirectMethods创建一个相同名字、接受相同参数的JavaScript函数。

例如,如果创建一个名为GetTimeStamp服务器端方法,那么在客户端,也会创建一个相应的Ext.net.DirectMethods.GetTimeStamp的JavaScript函数。

有种情况,当开发者创建了一个[DirectMethod],但是想隐藏相应的客户端JavaScript函数。此时,你可以在某个[DirectMethod]属性里设置它的ClientProxy.Ignore属性,从而忽略创建相应的客户端JavaScript函数。

如果[DirectMethod]设置为ClientProxy.Ignore,将不会创建相应的客户端代理函数(client-sideproxyfunction),但是[DirectMethod]仍然可以被调用。[DirectMethod]代理函数是围绕底层Ext.net.DirectMethod.request()函数的封装。

通过配置Ext.net.DirectMethod.request()函数,即便没有客户端代理函数,任何服务器端[DirectMethod]都可以被直接调用。

request(stringmethodName,[Objectoptions]):void

Callstheserver-side[DirectMethod]asspecifiedinthemethodNameparameter.

Parameters:

methodName:String

Theserver-sideMethodnametocall.

options:Object

(optional)Anobjectcontainingconfigurationproperties.Thisoptionsobjectmaycontainanyofthefollowingproperties,oroptionsasdefinedinExt.Ajax.request.

success:Function

TheJavaScriptfunctiontoinvokeonsuccessfulresponsefromtheDirectMethod.

The"result"parameterispassedtothesuccessfunction.

failure:Function

TheJavaScriptfunctiontoinvokeifafailureresponseisreturnedfromtheDirectMethod.

The"errorMessage"parameterispassedtothesuccessfunction.

specifier:String

Theserver-sideMethodaccessspecifier,optionsinlcude("public","static").

Thespecifierof"public"isthedefaultvalueanddoesnotneedtobeexplicitlyset.

Iftheserver-sideMethodisastaticMethod,thespecifieroptionsmustbesetto"static".

method:String

Thetypeofhttprequesttomake,optionsinclude("POST","GET").

Themethodof"POST"isthedefaultvalue.

url:String

AcustomurltocalltheDirectMethodfrom.TheDirectMethoddoesnotneedtobeconfiguredonthe"ParentPage".

Ifnourlisprovided,therequestoptionswillusethe<form>'sactionattribute.Iftheactionattributeisempty,therequestoptionswillusethewindow.location.hrefvalue.Ifthewindow.location.hrefvalueendswithaforward-slash("/"),theIISwebservermaynotbeabletoprocessthe"POST"request.Underthisscenario,youmustsetthe"method"optionspropertyto"GET".

control:String

TheIDoftheUserControlwhichcontainstheDirectMethod.AnDirectMethodcanbeconfiguredwithina.ascxfileandcalledfromaParent.aspxPage.

timeout:Number

Thetimeoutinmillisecondstobeusedforrequests.(defaultsto30000)

eventMask:Object

(optional)AnEventMaskoptionsobject.Thisoptionsobjectmaycontainanyofthefollowingproperties:

showMask:Boolean

truetoshowmask(defaultstofalse).

msg:String

Thetexttodisplayinacenteredloadingmessagebox(defaultsto'Working...').

msgCls:String

TheCSSclasstoapplytotheloadingmessageelement(defaultsto"x-mask-loading")

target:String

Thetargetelementtoapplythemaskto,optionsinclude("page","customtarget").

If"customtarget",thecustomTargetconfigurationoptionshouldbeset.

customTarget:String

Theidofthetargetelement,orainstanceofthetargetelement.

minDelay:Number

Theminimumamountoftimetodisplaythemask(defaultsto0).

SettingtheminDelayprovidesandminimumamountoftimetodisplayamessagetotheuserbeforeremovingmaskandexecutingsuccess,failureand/orcallbackfunctions.

Returns:

void


<scriptrunat="server">

[DirectMethod(ClientProxy=ClientProxy.Ignore)]

publicstringGetTimeStamp6()

{

returnDateTime.Now.ToLongTimeString();

}

</script>


<ext:Buttonrunat="server"Text="ClickMe"Icon="Lightning">

<Listeners>

<ClickHandler="Ext.net.DirectMethod.request(

'GetTimeStamp3',

{

success:function(result){

Ext.Msg.alert('Message',result);

}

});"/>

</Listeners>

</ext:Button>


向代理函数传递DirectMethod配置

DirectMethod配置对象可以被作为最后一个参数传递给任何DirectMethod代理函数。

<scriptrunat="server">

[DirectMethod]

publicstringLogMessage(stringmsg)

{

//Logthemessagesomewhere...

returnmsg;

}

</script>

<ext:ButtonID="Button4"runat="server"Text="ClickMe"Icon="Lightning">
<Listeners>

<ClickHandler="Ext.net.DirectMethods.LogMessage('HelloWorld',{

success:function(result){

Ext.Msg.alert('Message',result);

},

eventMask:{

showMask:true,

minDelay:500

}

});"/>

</Listeners>

</ext:Button>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: