Ext.net Study notes 07:ext.net directmethods usage

Source: Internet
Author: User

In the previous two articles, we saw the use of the directevents convenient call server-side method, Directevents call WebService method, today we look at Directmethods, This guy is more flexible than directevents, and it can invoke the server-side method asynchronously, just like the JS method.

Using Directmethods to invoke C # methods in JS

I admit that this title is a bit of a gimmick, in fact, it should be through the directmethods, in JS in the way of asynchronous invocation to execute the server-side method.

Let's look at an example:

[directmethod]  addtoservertime (hours) {    DateTime. Now.add (TimeSpan(hours, 0, 0));      This . MyLabel.Text = date. ToString ();}

This is a server-side method in an ASPX page, and in order to be able to invoke it in JS, you must add the Directmethod attribute.

Next, let's look at the JS call:

<type= "Text/javascript" >    fnupdate () {         App.direct.AddToServerTime (3);    } </ Script >

This is the sentence, it is defined in the namespace app.direct, of course, this namespace can be modified, I will tell you how to modify the next.

Then add a button to execute the Fnupdate method when the button is clicked.

<ext:Windowrunat= "Server"ID= "Win1"Width= "+"Height= "$"Title= "Directmethods" > <Items> <ext:Labelrunat= "Server"ID= "MyLabel" ></ext:Label> </Items> <Buttons> <ext:Buttonrunat= "Server"ID= "Btnupdate"Text= "Update"Handler = "fnupdate ()"></ext:Button> </Buttons></ext:Window>

We use the Ext.net button here, you can also use the ASP. NET button, or HTML button, no longer elaborate.

Then we compile the code, refresh the page, click on this button, and you will see the display in the window:

Set Directmethods namespace

By default, the Directmethods namespace is app.direct, and we can change the default namespace by using the N method:

Changing in the Ext.net global configuration
<theme="Neptune

Directmethodnamespace = " Myapp.directmethods

" />

This change is done in Web. config, and if you don't know how to add the Extnet configuration node in Web. config, you're definitely not looking at my first note. >> using ext.net in ASP. NET WebForm

This change will work for all pages .

Change in ext.net ResourceManager configuration
< ext :  runat= "Server"    directmethodnamespace= "Myapp.directmethods" ></ext:ResourceManager>

This change is done in the page, it is only valid for the current page, of course, it is definitely to overwrite the global settings.

Changing in the Directmethod attribute constructor

This is the last set-up, which is more flexible, and it can change the namespace of one method.

[ directmethod

)]addtoservertime (hours) {    DateTime . Now.add (TimeSpan(hours, 0, 0));      This . MyLabel.Text = date. ToString ();}

This method is only useful for the method that is set, which overrides the page and global configuration.

Add Callback method

As mentioned earlier, Ext.net Directmethods and directevents are similar, are asynchronous calls through the JS implementation, so if you want to handle the server-side execution structure, you must add the corresponding callback method in JS.

Fnupdate () {    MyApp.DirectMethods.AddToServerTime (5, {        success:onsuccess,        failure:onfailure,        }    });}  onsuccess () {    alert ("Success");}  onfailure () {    alert ("failed");}

In this code, we have added some configuration when invoking the Directmethod method, including a callback method to execute success and failure, and a mask layer to display.

Synchronous execution of server-side methods

In general, we are in an asynchronous way to execute the server-side approach, so that a better user experience, of course, if you have quirks, you want to be able to execute the server-side approach in a synchronous manner, Ext.net Directmethods is also supported.

[Directmethod(Namespace ="Myapp.directmethods", Async = false)]Public voidAddtoservertime (inthours) {    stringText = This. Btnupdate.text; vardate =DateTime. Now.add (NewTimeSpan(hours, 0, 0));  This. MyLabel.Text = Text + date. ToString ();}

A simple setup, configured in the Directmethod constructor, is fine.

Solemn reminder, as far as possible to use the synchronization of the request, this will cause the phenomenon of the browser suspended animation, as to what is the browser suspended animation, you try to rob the travel time of the train ticket will know (here is the 2014 Spring Festival train tickets, today is January 16, 2014, At present, 12306 of the verification code is synchronous request, do not know how many students are stuck in the verification code on the road ... )

Improving performance: Calling static methods

First of all, non-static methods, when executing a non-static method, Ext.net will submit more data so that we can access the controls on the page this is useful when dealing with page-related content, but when we just want to perform a process that is not related to the page, we can perform static methods to improve performance.

In a static method, we cannot access the form data of the page and cannot directly access the request, Response data (which can be accessed through httpcontext.current), the advantage is that Ext.net does not submit the page data to the server at the time of the request, greatly reducing the time for data transfer and server execution.

[directmethod"Myapp.directmethods")]  addtoservertime (hours) {     DateTime  . Now.add (TimeSpan(hours, 0, 0));      date. ToString ();}

This is the method we just made, and now we've changed it to a static method. JS Call basically did not change, but we need in JS to update the display of the control.

Fnupdate () {    MyApp.DirectMethods.AddToServerTime (5, {        success:onsuccess,        failure:onfailure,        }    });}  onsuccess (Result) {    App.myLabel.setText (result);}

We need to modify the Onsuccess callback method to update the interface display after it receives the execution result.

Ext.net.DirectMethod.request Call WebService

Directmethods does not have direct access to webservice, but can request webservice through Ext.net.DirectMethod.request to achieve the effect similar to the page background method.

() {    Ext.net.DirectMethod.request ({        "Echoservice.asmx/echo",        True  ,        params: {            "Hello world!"         },        (Result) {            alert (Ext.DomQuery.selectValue ("string" ""));        }    });}

This is the calling procedure for the method and is no longer demonstrated.

Additional instructions: This method is used for all webservice, general processing programs, MVC, etc. through the URL access to the east, very useful.

Exception handling

This is a big problem, in general, if you want to be lazy, ext.net by default has helped us with exception handling. Of course, if you want to be more friendly to the user to prompt, then to manually handle it.

[Directmethod]public static intDivide (intNumber1,intnumber2) {    if(number2 = = 0) {ResourceManager. Ajaxsuccess =false; ResourceManager. Ajaxerrormessage ="Number2 can ' t be zero!"; returnR; }    returnNumber1/number2;}

In this way, we manually determine whether the execution succeeds. Of course, you can also write:

[directmethod]  dosomething () {    try    {        //through to your business logic Layer    }    (e)    {        

ResourceManager . Ajaxsuccess = false

;         ResourceManager . Ajaxerrormessage = Getfriendlymessage (e);         logging and other operations     here }}

Ok,ext.net Directmethods The use of the introduction of so much, hope can help you.

Departure network original first, reproduced please indicate the source Oh ~

Original link: ext.net study notes 07:ext.net directmethods usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.