The difference between asp.net and ajax.net 1th/2 Page _ Basic Application

Source: Internet
Author: User
Tags javascript array microsoft c

Original:
Http://www.microsoft.com/china/msdn/library/webservices/asp.net/us0501ASPNETPerformance.mspx?mfr=true

Apply to:
AJAX (asynchronous JavaScript and XML)
Microsoft Ajax.NET
Microsoft asp.net

Summary: Learn how to use AJAX (asynchronous JavaScript and XML) with your Microsoft asp.net application to make it more interactive and responsive.

Download the code sample Ajaxaspnetcs.msi for this article (using the C # language)

Download the code sample Ajaxaspnetvb.msi for this article (using the Visual Basic language)

Content of this page

Brief introduction
What is AJAX?
AJAX for ASP.net
AJAX at hand
AJAX with You
Conclusion

Brief introduction

There has been a lot of trade-offs between Web applications and desktop applications since the beginning of Web programming. For example, it is generally assumed that WEB applications provide a user interface type that is not as rich as the user interface type provided by the desktop application. On the other hand, the WEB application is an independent platform, its development mechanism is relatively simple. Providing a more responsive application may seem like a simple task, but it is an area that the Web developer has been trying to conquer for a long time.

In the traditional sense, the response to user input can only be retrieved by submitting a new request to the WEB server. In some cases, developers can use JavaScript to load all the responses on the client, providing a better user experience. A common example of this technique is to dynamically load a range of states or provinces based on the selected country/region. Unfortunately, in many cases, it is better not to return or load all responses to JavaScript. The return operation causes too much UI to be disconnected, or excessive data is required on the client, which often results in the generation of unreadable JavaScript. AJAX provides a new intermediate option to leverage server-based applications while maintaining timely responsiveness and flexibility.

Back to the top of the page

What is AJAX?

AJAX is the abbreviation for asynchronous JavaScript and XML (asynchronous JavaScript and XML), not a technology, but a collection of many techniques. AJAX uses communication technology (usually SOAP and XML) to send and receive asynchronous requests/responses to the server, and then processes the response with display technology (JavaScript, DOM, HTML, and CSS). Today, applications that use AJAX are legal because most browsers support this required technology. For more detailed definition of Ajax, visit the Ajax Wikipedia entry(English).

What exactly is AJAX? AJAX enables you to execute server-side methods through JavaScript, without refreshing the browser. Consider it a small request/response that occurs in the background of the user. If you're still not sure what AJAX is, see the two common examples on Google:Google suggests(English) and Google Maps. If you are unfamiliar with AJAX, the response of these two applications can be a little exciting.

Back to the top of the page

AJAX for ASP.net

Many factors have prompted AJAX to emerge. You might not want to spend hours or days understanding the internal workings of Ajax, but rather start creating AJAX-enabled applications now to meet your existing needs (if you really want to know how Ajax works, I'm certainly not the person to ask). There are a number of tools that developers can use to get started quickly. However, we will pay special attention to the free ajax.net written by Michael Schwarz to open source code. Ajax.NET takes into account that all implementation details are based on. NET and can be extended. Microsoft asp.net 2.0 introduced a unique asynchronous callback with the client callback feature , and recently announced that AJAX, code-named "Atlas", is being implemented.

The terminology may be a bit confusing, but when I introduce AJAX, it's about the overall framework of calling server-side functions asynchronously from the client. When it comes to ajax.net, I mean a specific implementation that can help you create a solution that leverages the Ajax framework.

To learn more about the ASP.net 2.0 client callback feature, please visit Bertrand Le Roy 's blog (English).

Back to the top of the page

AJAX at hand

The remainder of this article will use Ajax.NET to highlight three meaningful examples that utilize AJAX functionality. This guide will contain code written in Microsoft C # and Microsoft Visual Basic. NET, sometimes providing both code, and sometimes just one of the code. Implementing all of this code is easy, and C # developers can easily follow code written only in Visual Basic. NET, and vice versa! This article contains examples of C # and Visual Basic. NET projects that are available for download and provide work code and run code. Before learning the example, you first need to know how to install and use Ajax.NET.

Ajax.NET

Ajax.NET documents (English) and Web sites are useful for developers to get started quickly. Before we introduce some specific examples of using this technology, we'll briefly review the core steps you need to know.

Download and extract the AJAX files from the ajax.net Project website First, and then create a new ASP.net project in Visual Basic. NET or C # to your liking, and then to the AJAX.dll file add Reference (English). The only additional configuration step is to add the following code in the <system.web> element (located in the web.config file).

<configuration>
<system.web>

To make server-side functions available in JavaScript, you have to do two things. First, the function to be used must be marked with ajax.ajaxmethodattribute. Second, during page load events, you must invoke Ajax. Utility.registertypeforajax to register the class that contains the functions. It may sound a bit complicated, but don't worry; you just need to add two more lines to your code. Let's look at an example.

C # Public
class Sample:System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{
//Register the
class
Ajax.Utility.RegisterTypeForAjax (typeof (Sample) that we are interested in containing server-side functions//);
}
[Ajax.ajaxmethod ()]
public string GetMessageOfTheDay ()
{return
"experience is the mother of Wisdom";
}
}
' vb.net public
Class Sample
Inherits System.Web.UI.Page
Private Sub Page_Load (Sender Asobject, e as EventArgs)
Handles mybase.load
' registers the class Ajax.Utility.RegisterTypeForAjax which we are interested in including server-side functions
'
( GetType (Sample)) End
Sub
<ajax.ajaxmethod () > _ Public
Function GetMessageOfTheDay () as String Return
' experience is the mother of Wisdom '
End Function End
Class

The above example first tells Ajax.NET to look for friendly Ajax methods in the sample class. It happens to be the same class as the actual page, but it can be any. NET class, or you can register multiple classes. Ajax.NET then browses to the specified class to find all the methods labeled AjaxMethodAttribute , where the Sample class has a getmessageoftheday.

When you're done, the only thing left to do is use it in JavaScript. Ajax.NET automatically creates a JavaScript variable with the same name as the registered class (in this case, sample), which provides a function with the same name as Ajaxmethod (in this case GetMessageOfTheDay). as shown below.

<script language= "JavaScript" >
sample.getmessageoftheday (getmessageoftheday_callback);
function Getmessageoftheday_callback (response)
{
alert (response.value);
}
</script>

In addition to JavaScript callback functions, JavaScript getmessageoftheday requires the same parameters as the corresponding portions of its server side (in this case, no parameters) to execute and deliver the response at completion. Here, we see the asynchronous nature of AJAX at work, because calls to GetMessageOfTheDay do not hinder the execution of other JavaScript code, nor do they prevent users from continuing to operate on the page. When server-side processing is complete, ajax.net invokes the specified callback function Getmessageoftheday_callbackand passes a response composed of server-side return values.

The mapping between server-side code and JavaScript code can be confusing. Figure 1 shows a brief overview of the server-side code and JavaScript code, as well as the mapping between the two.


Figure 1 : Server-side code and JavaScript Mapping between Code

Of course, interesting ajax.net there are more to be covered, such as support for. NET types and Rich callback responses (it's not just values). The following example will focus on some features that you want to help you understand how AJAX can help you create successful applications.

Sample 1 : Drop-down List of links

The beginning of this article briefly discusses two traditional methods for linking two DropDownList . Returns the page when the selected index changes, or loads all possible data into a JavaScript array and displays it dynamically. Hopefully, you'll see how AJAX can replace both of these solutions.

First, let's take a look at our data interface and drive the sample from the data interface. Our data access layer will provide two methods: the first will retrieve the list of countries/regions supported by the system, and the second method will get the country/region ID and return to the list of States/provinces. Since this is pure data access, we only need to use the method.

C # public
static DataTable getshippingcountries ();
public static DataView getcountrystates (int countryid);
' vb.net public
shared function getshippingcountries () as DataTable public
shared function getcountrystates ( ByVal Countryid as Integer) as
DataView

Now, let's go to the opposite side and create a simple Web form.

<asp:dropdownlist id= "Countries" runat= "server"/> <asp:dropdownlist "id=" states "
server" runat=
<asp:button id= "Submit" runat= "Server" text= "Submit"/>

Page_Load events are just as simple as the Web forms described earlier. We use the data access layer to retrieve the available country/region and bind it to countriesdropdownlist .

C #
if (! Page.IsPostBack)
{
countries. DataSource = DAL. Getshippingcountries ();
Countries. DataTextField = "Country";
Countries. DataValueField = "Id";
Countries. DataBind ();
Countries. Items.insert (0, New ListItem ("Please select", "0"))

Usually, the code ends here. First, we'll create a server-side function to invoke from JavaScript.

' VB.net
<ajax.ajaxmethod () > _ Public
Function getstates (ByVal Countryid as Integer) as DataView
Return DAL. Getcountrystates (Countryid) End
Function

Current 1/2 page 12 Next read the full text
Related Article

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.