A simple example of Using ajax to obtain time on the server

Source: Internet
Author: User

First, you must reference a class library ajaxpro2.dll,

Register Ajax in the background code on the page, create a serverservice class, and add the [ajaxmethod] label to the class method.

Ajaxpro. Utility. registertypeforajax (typeof (serverservice); // register

Serverservice class:

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 serverservice
/// </Summary>

[System. serializable]
Public class selectitem
{
Private string value;

Private string text;

Public String text
{
Get {return text ;}
Set {text = value ;}
}

Public String Value
{
Get {return value ;}
Set {value = value ;}
}

}

Public class serverservice
{
Public serverservice ()
{
//
// Todo: add the constructor logic here
//
}

// Return the time on the server
[Ajaxpro. ajaxmethod]
Public String getservertime ()
{
System. Threading. thread. Sleep (6000 );

Return datetime. Now. tolongtimestring ();
}

[Ajaxpro. ajaxmethod]
Public void receive (string message)
{
System. Collections. Generic. List <string> List
= Httpcontext. Current. application ["reply"] As system. Collections. Generic. List <string>;

List. Add (Message );
}

[Ajaxpro. ajaxmethod]
Public String [] getallreply ()
{
System. Collections. Generic. List <string> List
= Httpcontext. Current. application ["reply"] As system. Collections. Generic. List <string>;
Return list. toarray ();
}

[Ajaxpro. ajaxmethod]
Public selectitem [] getitems (string ID)
{
// Generate simulated data
Selectitem [] items = new selectitem [8];
For (INT I = 0; I <items. length; I ++)
{
Items [I] = new selectitem ();
Items [I]. value = string. Format ("{0} 00 {1}", ID, I );
Items [I]. Text = string. Format ("Project-{0}", I );
}

Return items;
}

}

 

Put a global file:

<% @ Application language = "C #" %>

<SCRIPT runat = "server">

Void application_start (Object sender, eventargs E)
{
// Code that runs when the application starts

System. Collections. Generic. List <string> List
= New system. Collections. Generic. List <string> ();

Httpcontext. Current. application ["reply"] = List;

}

Void application_end (Object sender, eventargs E)
{
// Code that runs when the application is closed

}

Void application_error (Object sender, eventargs E)
{
// Code that runs when an unhandled error occurs

}

Void session_start (Object sender, eventargs E)
{
// The code that runs when the new session starts

}

Void session_end (Object sender, eventargs E)
{
// The code that runs when the session ends.
// Note: Only the sessionstate mode in the web. config file is set
// The session_end event is triggered only when inproc is used. If the session mode is set to StateServer
// Or sqlserver, the event is not triggered.

}

</SCRIPT>

 

Page to reference the jquery. Min. js file:

<% @ 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>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Input type = "button" value = "get time on the server" onclick = "getservertime ();"/>
</Div>
</Form>

<SCRIPT type = "text/JavaScript">

Function getservertime ()
{
/*
// Synchronous Mode
VaR result = serverservice. getservertime ();
Alert (result. value );
*/

// Asynchronous mode
Serverservice. getservertime (
Function (result)
{
Alert (result. value );
}
);

}

</SCRIPT>

</Body>
</Html>

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.