To invoke the Web service's application method from the script using ASP.

Source: Internet
Author: User

Skill point: By writing WebService, call WebService in the page JS to query the data.

Website development, sometimes need to use JS to dynamically generate some content on the page, but there are some data to be retrieved by querying the database.

However, some key data, such as primary key ID, are not easy to get in the background, or it is not easy to use JS directly in the foreground after obtaining data in the background.

In this case, the use of Js+ajax way to achieve, the effect is also good.

1. Write WebService:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.Web.Services;6 usingBLL;7 usingModel;8 ///9 ///Summary description of GetpropointserviceTen /// One[WebService (Namespace ="http://tempuri.org/")] A[WebServiceBinding (ConformsTo =wsiprofiles.basicprofile1_1)] - //to allow the Web service to be called from the script using ASP. NET AJAX, uncomment the downstream.  - [System.Web.Script.Services.ScriptService] the  Public classGetProPointService:System.Web.Services.WebService { -      PublicGetpropointservice () { -         //If you are using a design component, uncomment the following line -         //InitializeComponent (); +     } - [WebMethod] +      Public intGetpointbyprono (intProno) A     { atPROSELECTBLL BLL =NewPROSELECTBLL (); -         returnBLL. Getpointbyprono (Prono); -     } -}

2. Register the Web service in the foreground page code:

1         <asp:scriptmanager id="ScriptManager" runat="server " >2             <Services>3                 <asp:servicereference path="~/ Webservice/getpropointservice.asmx" />4             </Services>5         </asp:scriptmanager

3. Invoke the Web service in the foreground page script:

     // event handler function      function Btninvoke_onclick () {         var thename = $Get("tbname " ). Value;         Simplewebservice.sayhello (thename, processrollback);     }      // callback function      function Processrollback (result) {         $get("result" ). InnerHTML = result;     }

Extension: If the loop call WebService interacts with the database, that is, the Web service is looped in the event handler, then the obtained data will be performed one after the end of the loop of the event handler, such as the following example:

// event handler function   for (var03; i++) {     // loop      Service.servicemethod (processrollback);     Alert ("test");} // callback function function Processrollback (result) {     alert (result);}

Supposedly, it should be "test", and the value of result is a,b,a,b,a,b in turn, but the result here is that it pops up 3 times and then the value of result 3 times.

It is equivalent to an event handler that calls WebService every time a loop is passed, or passes a parameter webservice, but WebService does not loop with the event handler, but instead acts as expected after the event handler ends.

As the current knowledge of the Web service is not deep enough to explain this phenomenon, but it is supposed to be followed by the event handler loop operation in the callback function to loop the method to achieve the function I want.

To invoke the Web service's application method from the script using ASP.

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.