Differences between Asp. Net and AJAX. Net page 1/2

Source: Internet
Author: User
Tags javascript array microsoft c


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

Applicable:
AJAX (Asynchronous JavaScript and XML)
Microsoft AJAX. NET
Microsoft ASP. NET

Abstract:Learn how to use AJAX (Asynchronous JavaScript and XML) in your Microsoft ASP. NET application to make it more interactive and responsive.

Download the code example AjaxASPNETCS. msi (in C)

Download the sample code AjaxASPNETVB. msi (in Visual Basic)

Content on this page
Introduction
What Is AJAX?
AJAX for ASP. NET
AJAX at hand
AJAX and you
Conclusion
Introduction

Since the beginning of Web programming, there have been many trade-offs between Web applications and desktop applications. For example, the user interface types provided by Web applications are generally not as rich as those provided by desktop applications. On the other hand, Web applications are independent platforms with simple development mechanisms. Providing more responsive applications seems to be a simple task, but it is always a field for Web developers.

Traditionally, you can only submit new requests to the Web server to retrieve the responses to user input. In some cases, developers can use JavaScript to load all responses on the client to provide a better user experience. A common example of this technology is to dynamically load a series 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 may cause too many UIS to be disconnected or excessive data is required on the client, which often leads to the generation of JavaScript that is not easy to read. AJAX provides a new intermediate choice that allows you to utilize server-based applications while maintaining timely response and flexibility.

What Is AJAX?

AJAX is short for Asynchronous JavaScript And XML (Asynchronous JavaScript And XML). It is not a technology, but a collection of many technologies. AJAX uses communication technology (usually SOAP and XML) to send and receive asynchronous requests/responses to the server, and then uses the Display Technology (JavaScript, DOM, HTML, and CSS) to process responses. Today, AJAX applications are legal because most browsers support this required technology. For more detailed AJAX definitions, visitAJAX Wikipedia entry(English ).

What Is AJAX? AJAX allows you to use JavaScript to call and execute server-side methods without refreshing the browser. It is regarded as a small request/response that occurs in the user's background. If you still do not know what AJAX is, see two common examples on Google:Google Suggests(English) andGoogle Maps(English ). If you are not familiar with AJAX, the response from these two applications will make you a little excited.

Back to header AJAX for ASP. NET

Many factors have prompted AJAX to emerge. You may not want to spend a few hours or a few days learning about the internal principles of AJAX. Instead, you want to create an AJAX-enabled application now, to meet the existing needs (if you really want to know how AJAX works internally, I am certainly not the one to ask ). There are many tools that developers can use to get started quickly. However, we will pay special attention to the free Ajax. NET open source code written by Michael Schwarz. Ajax. NET considers that all implementation details are based on. NET and can be extended. Microsoft ASP. NET 2.0Client callbackA unique asynchronous callback is introduced andRecently announcedAJAX code "Atlas" is being implemented.

The term may be a bit confusing, but when I introduced AJAX, I was introducing the overall framework for Asynchronously calling server-side functions from the client. When it comes to Ajax. NET, I mean the specific implementation that helps you create a solution that uses the AJAX framework.

For more information about ASP. NET 2.0 client callback, visitBertrand Le RoyBlog(English ).

Back to Top AJAX

The rest of this article will introduce three meaningful examples using Ajax. NET. This guide will contain code written in Microsoft C # and Microsoft Visual Basic. NET, sometimes both of which are provided at the same time, and sometimes only one of them. It is easy to implement all the code. C # developers can easily follow the code written only in Visual Basic. NET, and vice versa! The example C # and Visual Basic. NET projects in this article can be downloaded, and work code and run code are provided. Before learning examples, you must first understand how to install and use Ajax. NET.

Ajax. NET

AJAX. NETDocument(English) andWebsiteIt is very useful for developers to get started quickly. Before introducing specific examples of using this technology, we will briefly review the core steps you need to know.

FirstAJAX. NETProject WebsiteDownload and decompress the AJAX file, create an ASP. NET project in Visual Basic. NET or C # according to your preferences, and then open the AJAX. dll fileAdd reference(English ). The only additional configuration step is in<System. web>Element (located inWeb. configFile) Add the following code.

<configuration>
<system.web>
<httpHandlers>
<!-Register the ajax handler->
<add verb = "POST, GET" path = "ajax / *. ashx"
type = "Ajax.PageHandlerFactory, Ajax" />
</ httpHandlers>
...
...
</system.web>
</ configuration>
To make server-side functions available in JavaScript, two things must be done. First, the function to be used must be labeled Ajax.AjaxMethodAttribute. Second, during the page load event, the class containing these functions must be registered by calling Ajax.Utility.RegisterTypeForAjax. This may sound complicated, but don't worry; you really only 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 server-side functions we are interested in
//the type
Ajax.Utility.RegisterTypeForAjax (typeof (Sample));
}
[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
'Register the server-side functions we are interested in
'the type
Ajax.Utility.RegisterTypeForAjax (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 find 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 will then browse the specified classes to find all the methods labeled AjaxMethodAttribute, where the Sample class has a GetMessageOfTheDay.

Once that's 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 (Sample in this example), which provides a function with the same name as AjaxMethod (GetMessageOfTheDay in this example). As follows.

<script language = "javascript">
Sample.GetMessageOfTheDay (GetMessageOfTheDay_CallBack);
function GetMessageOfTheDay_CallBack (response)
{
alert (response.value);
}
</ script>
In addition to the JavaScript callback function, JavaScript GetMessageOfTheDay requires the same parameters as its server-side counterpart (in this case, no parameters) in order to execute and pass the response upon completion. Here we see the asynchronous nature of AJAX at work, because the call to GetMessageOfTheDay does not prevent the execution of other JavaScript code, nor does it prevent the user from continuing to operate on the page. When server-side processing is complete, Ajax.NET calls the specified callback function GetMessageOfTheDay_CallBack and passes it a response consisting of the server-side return value.

The mapping between server-side code and JavaScript code can be a bit confusing. Figure 1 briefly shows the server-side and JavaScript code, and the mapping between the two.


Figure 1: Mapping between server-side code and JavaScript code

Of course, there is much more interesting Ajax.NET, such as support for .NET types and rich callback responses (it's not just values). The following examples highlight some of the features and hope to help you understand how AJAX can help you create successful applications.

Example 1: Linked drop-down list

The beginning of this article briefly discussed two traditional methods for linking two DropDownLists. Return to the page when the selected index changes; or load all possible data into a JavaScript array and display it dynamically. Hope you can see how AJAX can replace these two solutions.

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

// 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 move to the opposite side and create a simple Web form.

<asp: DropDownList ID = "countries" Runat = "server" />
<asp: DropDownList ID = "states" Runat = "server" />
<asp: Button ID = "submit" Runat = "server" Text = "Submit" />
The Page_Load event is just as simple as the previous Web Form. We use the data access layer to retrieve the available countries and bind them 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 will create a server-side function to be called from JavaScript.

'VB.NET
<Ajax.AjaxMethod ()> _
Public Function GetStates (ByVal countryId As Integer) As DataView
Return DAL.GetCountryStates (countryId)
End Function


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.