Ajax development example (Ajax. dll and. NET 2.0)

Source: Internet
Author: User

Using ajax to develop Web applications has become the most common method, which can bring a better user experience. I also downloaded an Ajax. dll file from the Internet.

1. Download Ajax. dll, which is available everywhere.

Http://ajax.schwarz-interactive.de/CSharpSample/

2. Create a website project ajaxsample

3. decompress the downloaded aajx. DLL to the project (the directory is not limited)

4. Reference Ajax. DLL to the project.

Fifth, add the Ajax. dll section in Web. config:

<Httphandlers>
<Add verb = "post, get" Path = "ajax/*. ashx" type = "Ajax. pagehandlerfactory, Ajax"/>
</Httphandlers>

The above configuration must be placed in <system. Web>.

Sixth, because this is a test project, we do not consider too many architecture issues, but to separate the business and performance, we create a class file test. CS: the service code is concentrated in this file. Class files are stored in the app_code directory.

Class file:

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

/** // <Summary>
/// Summary of test
/// </Summary>
Public class test
...{
Public test ()
...{
//
// Todo: add the constructor logic here
//
}

[Ajax. ajaxmethod ()]
Public String gettext ()
...{
Return "this is an Ajax test ";
}
}

7. Create a new page file default. aspx and default. aspx. CS Code as follows:

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class _ default: system. Web. UI. Page
...{
Protected void page_load (Object sender, eventargs E)
...{
Ajax. Utility. registertypeforajax (typeof (TEST ));
}
}

Default. aspx is as follows:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>

<Script language = "JavaScript">

Function ajaxtest ()
...{
VaR A = test. gettext (). value;
Window. Alert ();
}

</SCRIPT>

</Head>
<Body onload = "ajaxtest ();">
<Form ID = "form1" runat = "server">
</Form>
</Body>
</Html>

After running, you can see the final effect.

There are several notes:

First, all functions that need to be called in Javascript need to be added to the function [Ajax. ajaxmethod ()] (For details, refer to other files)

Second, all classes of functions that are called in Javascript must be described in page_load:

Ajax. Utility. registertypeforajax (typeof (TEST ));

Test indicates the service class name.

The above is just a simple call method. I think it will take some effort to learn Ajax well!

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.