Simple examples and notes of Ajax. Net pro

Source: Internet
Author: User
★BackgroundCodeAs follows:
Bytes -----------------------------------------------------------------------------------------------------------

1 Public Partial Class Ajaxpro_example_default: system. Web. UI. Page
2 {
3
4 Protected   Void Page_load ( Object Sender, eventargs E)
5 {
6Ajaxpro. Utility. registertypeforajax (Typeof(Ajaxpro_example_default ));//You must ensure that this registration statement is executed every time you send the message back.
7}
8
9 [Ajaxpro. ajaxmethod]
10 Public Datetime getservertime ()
11 {
12ReturnDatetime. now;
13}
14 }

★The front-end code is as follows: after the page is loaded, the time is acquired from the server every second and displayed in divmsg on the aspx page.
Bytes ----------------------------------------------------------------------------------------------------

1 < Head runat = " Server " >
2 < Title > No title page </ Title >
3 < Script Type = " Text/JavaScript " >
4 Function getservertime ()
5 {
6 // Ajaxpro_example_default.getservertime (getservertime_callback ); // Asynchronous call comment out: original code in the example
7
8 Window. setinterval ( " Ajaxpro_example_default.getservertime (getservertime_callback) " , 1000 ); // Call once every second
9 }
10
11 // Callback Function
12 Function getservertime_callback (RES)
13 {
14 // Alert (res. value ); // Comment out: original code in the example
15 Document. getelementbyid ( " Divmsg " ). Innertext = Res. value; // Add the time value to the DIV for rendering.
16 }
17
18 Window. onload = Getservertime; // Executed during page loading
19
20 </ Script >
21 </ Head >
22 < Body >
23 < Form ID = " Form1 " Runat = " Server " >
24 < Div ID = " Divmsg " Style = " Width: 100px; Height: 100px " >
25 </ Div >
26 </ Form >
27 </ Body >
28 </ Html >

★Several important points worth attention:
Bytes ----------------------------------------------------------------------------------------------------
1. It must be used in Web ApplicationsProgramIn the Web. config file under the root directory<System. Web>Add:
<Httphandlers>
<Add verb = "post, get" Path = "ajaxpro/*. ashx" type = "ajaxpro. ajaxhandlerfactory, ajaxpro.2"/>
</Httphandlers>

2. The server must register classes that contain methods in page_load, and ensure that the registration statement is executed every time the page is sent back.
It cannot be written as follows:
If (! Ispostback)
{// The registration statement cannot be written here. Otherwise, the client will not be able to access the ajaxpro_example_default class after the page is sent back.
Ajaxpro. Utility. registertypeforajax (typeof (ajaxpro_example_default ));
}

3. The server is only used[Ajaxpro. ajaxmethod]The method marked before it can be called by the client

4. The client page must contain<Form runat = "server">Mark. Otherwise, you cannot access the registered classes on the server.

★Regret:
Bytes ----------------------------------------------------------------------------------------------------
It cannot be called across pages. You can only access the registered class or method on the foreground page associated with the descendant CS, if it can be in one place (such as in the global file) register all the objects that need to be accessed at the front end, and the registered objects can be accessed on any HTML or ASPX page at the front end, which is more convenient and flexible.

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.