Asp.net Ajax 1.0, hello World Program

Source: Internet
Author: User

Asp.net Ajax is very different from Atlas. In this simple example, we can see several points.
<1> Create an Asp.net Ajax-enabled web site
<2> page layout. The tag prefix of server controls is changed from Atlas to ASP;

<% @ Page Language = " C # " Autoeventwireup = " True " Codefile = " Default. aspx. CS " Inherits = " Helloworld "   %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.1 // en" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
< Html >
< Head Runat = "Server" >
< Title > Hello </ Title >
</ Head >
< Body >
< Form ID = "Form1" Runat = "Server" >
< ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server" >
< Services >
< ASP: servicereference Path = "~ /Helloworldservice. asmx"   />
</ Services >
</ ASP: scriptmanager >
< Div >
Your name:
< Input Type = "Text" Maxlength = "20" ID = "Name"   />
< Input Type = "Button" ID = "Button1" Value = "Greetings" Onclick = "Sayhello ()"   />
< Div ID = "Result" > </ Div >
</ Div >
</ Form >
</ Body >
</ Html >

<3> client script. The method for calling the service is somewhat changed. You can specify the default callback method. < Script Type = " Text/JavaScript " >
Function Sayhello ()
{
VaRFS=Helloworldservice;
FS. set_defaultsucceededcallback (onshow );
FS. helloworld (document. getelementbyid ("Name"). Value );
}
Function Onshow (result)
{
VaRS=Document. getelementbyid ("Result");
S. innertext=Result;
}
</ Script >

<4> helloworldservice Code . To enable the Service to be called by the Asp.net Ajax client, you must specify the [scriptservice] attribute for the Service (to use this attribute, you must reference the Microsoft. Web. Script. Services namespace ). 1 Using System;
2 Using System. Web. Services;
3 Using System. Web. Services. Protocols;
4 Using Microsoft. Web. Script. Services;
5
6 [WebService (namespace =   " Http://tempuri.org/ " )]
7 [Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
8 [Scriptservice]
9 Public   Class Helloworldservice: system. Web. Services. WebService {
10
11 Public Helloworldservice () {
12
13//Uncomment the following line if using designed components
14//Initializecomponent ();
15}
16
17 [Webmethod]
18 Public   String Helloworld ( String Name) {
19 String Hello = String. isnullorempty (name) ?   " Anonymous " : Name;
20 Hello + =   " Hello, the current server time is: " ;
21 Hello + = Datetime. Now. touniversaltime ();
22 Return Hello;
23 }
24
25 }

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.