Asp. NET in JavaScript call back code

Source: Internet
Author: User
Tags call back

Pre-order: In the case of no need to right-click the page or postback, the page is refreshed, that is, JS call background code;


Method One: Invoke a Click event that hides the service-side button

1. Put a hidden button on the front end to write the backend code that needs to be called into the onclick event;

2, write a JS function in the foreground, function body for document.getElementById ("<%=Btnid. Clientid%> "). Click ();

3, the need to refresh the place to call the JS function, you can invoke the button OnClick event;

4, this method applies no return value;


Method Two: JS directly with <%=methodname ()%> call

1, in the background to write a good method, public or protected;

       2, write a JS function in the foreground, the function body is Var result = "<%= MethodName ()%> "or direct eval (" <%=< Span style= "font-size:18px" > < Span style= "font-size:18px" >methodname ()%> ");  

3, the need to refresh the place to call the JS function, you can invoke the background method;

4, this method is applicable with the return value;


method Three : using Pagemethod call


Pagemethod Method Description:

Pagemethods.functionname (Paramter1,parameter2,..., successmethod, Failedmethod, UserContext);
Where the preceding Paramter1,parameter2,..., represents the functionname parameter, the type is object or array;
Successmethod is the JS method that needs to use the background return result
Failedmethod is the JS method (a fault-tolerant approach) that executes when the Csfunctionname method in the background has an exception.
UserContext is any content that can be passed to the Successmethod method, or Failedmethod method.


Implementation method Three follow these steps:

1. The background method must be public static,

The method adds [System.Web.Services.WebMethod] to the head to label the attribute.

2. Add the ScriptManager control to the foreground page and set its Enablepagemethods property to True.

3. Call Pagemethods, here is just the simplest call.

Pagemethods.functionname (The JS method of callback);

where functionname is created for the background static method name, callback JS method for the foreground to accept the results of the method.


Pagemethods Example:

Background code:

I. No-parameter method

[System.web.services.webmethod]public static string Showvalue ()//Can be added with parameter {return "helloword=  =c#";}

Front-End Code:

<script type= "Text/javascript" > Function bclick () {pagemethods.showvalue (Helloword);// Call the Parameter method Pagemethods.showvalue (' 123 ', Helloword);} function Helloword (Result)//callback method accepts the execution result of the background code {alert (result);} Call background has parameter method function Bclick2 () {var text = "Test"; Pagemethods.showvalue2 (TEXT,SSHOW2);} </script>


Method Four: JQuery Ajax asynchronous invocation

Baidu a big pile

Asp. NET in JavaScript call back code

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.