Use scriptmanager to call the server method or WebService method on the client.

Source: Internet
Author: User
I haven't been writing for a long time. I have been busy with my work recently. In addition to the normal work requirements, I have to prepare some documents, ppt and demo, I also spent a lot of time preparing for this Ajax training. The training was finally completed yesterday, and it seemed as easy to handle the requirements. Looking back at the recent work, there were not many problems. Let's take out several client calls to the server today:
1. The client calls the server method: Code in the source file
1   < Script Language = Javascript type = " Text/JavaScript " >
2 Function getcurrenttime ()
3 {
4Pagemethods. getcurrenttime ('Newegg Ajax Training', Checkissuccess );
5}
6 Function checkissuccess (result)
7 {
8Alert (result );
9}
10 </ Script >
11
12 < Input ID = " Button1 " Type = " Button " Value = " Client controls call server Methods " Onclick = " Getcurrenttime () "   /> Server Method
1 [Webmethod]
2 Public   Static   String Getcurrenttime ( String Str)
3 {
4ReturnStr+Datetime. Now. tolongtimestring ();
5}

In this case, the scriptmanager has an attribute: enablepagemethods = "true"
2. The client calls WebService:Source code:
1   < ASP: scriptmanager ID = " Scriptmanager1 " Runat = " Server " >
2 < Services >
3 < ASP: servicereference path = " Ajaxtestwebservice. asmx " Inlinescript = " True "   />
4 </ Services >
5 </ ASP: scriptmanager >
6 < BR />
7 This is to use the client method to call the methods in WebService. & Nbsp; < Div >
8 < Input ID = " Button1 " Type = " Button " Value = " Click the method in WebService to generate a random number. " Onclick = " Getrandom () "   />
9
10 < Script Language = " Javascript " Type = " Text/JavaScript " >
11 Function getrandom ()
12 {
13Ajax_dear.ajaxtestwebservice.getrandom (getr );
14}
15 Function getr (result)
16 {
17Alert (result );
18}
19 </ Script > WebService Method
1 [WebService (namespace =   " Http://tempuri.org/ " )]
2 [Webservicebinding (conformsto = Wsiprofiles. basicprofile1_1)]
3 [Scriptservice]
4 Public   Class Ajaxtestwebservice: system. Web. Services. WebService
5 {
6
7 [Webmethod]
8 Public   String Helloworld ()
9 {
10Return "Hello World";
11}
12 [Webmethod]
13 Public   Int Getrandom ()
14 {
15Return NewRandom (datetime. Now. millisecond). Next ();
16}
17 }

There is nothing to say about it,CodeAll problems,

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.