Asynchronous transmission of. net ajax data

Source: Internet
Author: User
First, you must create a website to create the following files: Web forms (Default. asp tutorial x), JScript File (ajax. js), Web service (SayHelloService. asmx), Class (Hello. cs)
(Add: you need to add a Microsoft. Web. Preview. dll file to download it from CSDN)
 
  • Default. aspx front-end code:
<Supplement> you need to create a new ScriptManager control to partially refresh ajax data.
  
<% @ 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> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: ScriptManager ID = "ScriptManager1" runat = "server">
<Scripts>
<Asp: ScriptReference Assembly = "Microsoft. Web. Preview" Name = "PreviewScript. js"/>
<Asp: ScriptReference Path = "~ /Ajax. js "/>
</Scripts>
<Services>
<Asp: ServiceReference Path = "~ /SayHelloService. asmx "/>
</Services>
</Asp: ScriptManager>

<Div>
<Input id = "btnSayHello" type = "button" value = "SayHello"/>
<Div id = "result"> </div>
</Div>
</Form>
</Body>
</Html>
  • Ajax file code:
<Supplement> the role of Preview. dll is here, in order to make the compatriots who are used to the background encoding of the asp.net tutorial feel familiar ....
  
     
Var btnSayHello;
Var lblResult;

Sys. Application. add_init (onPageInit );
 
 
 


Function OnFailded (error)
{
LblResult. set_text ("Call failed. Error message: "+ error. get_message ());
} Function OnSucceeded (resultText)
{
LblResult. set_text (resultText );
}
Function btnSayHello_onClick ()
{
SayHelloService. SayHello (OnSucceeded, OnFailded );
} Function onPageInit ()
{
BtnSayHello = new Sys. Preview. UI. Button ($ get ("btnSayHello "));
BtnSayHello. initialize ();

LblResult = new Sys. Preview. UI. Label ($ get ("result "));
LblResult. initialize ();

BtnSayHello. add_click (btnSayHello_onClick );
}
Code of the SayHelloService. cs file:
<Supplement> the function of this Web service class is to construct a "communication platform" between js files and common classes ".
In fact, this can also be done in basic classes. However, to a certain extent, you must add keywords such as [ScriptService] and [WebMethod] to the basic classes to "pollute" the basic classes.
Remember to add keywords such as [ScriptService] and [WebMethod]. Remember to add the [WebMethod] keyword before every time you write a function.
  
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
Using System. Web. Script. Services;



[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[ScriptService]
 


[WebMethod]

Public string SayHello ()
{
Hello myHello = new Hello ();
Return myHello. SayHello ();
}
} Public class SayHelloService: System. Web. Services. WebService {

Public SayHelloService (){

}
Hello code
  
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;


Public class Hello
{
Public Hello ()
{
}

Public string SayHello ()
{
Return "Hello! Anna ";
}

}
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.