JavaScript in ASP. NET calls the background code, asp. netjavascript

Source: Internet
Author: User

JavaScript in ASP. NET calls the background code, asp. netjavascript

Preface: refresh the page without right-clicking the page or sending back, that is, JS calls the background code;


Method 1: Call the click event to hide the server button

1. Place a hidden button on the front end to write the background code to The OnClick event;

2. Write a js function at the front end. The function body is document. getElementById ("<% = btnID. ClientID %>"). click ();

3. Call the js function where you need to refresh it To Call The OnClick event;

4. This method is applicable to non-return values;


Method 2: directly use <% = MethodName () %> In JS to call

1. Write the method public or protected in the background;

2. Write a js function on the front end. The function body is var result = "<% = MethodName () %>" or directly eval ("<% = MethodName () %> ");

3. Call the js function where a refresh is needed to call the background method;

4. This method is applicable to return values;


Method 3: Use PageMethod to call


PageMethod Method Introduction:

PageMethods. FunctionName (Paramter1, Parameter2,..., SuccessMethod, FailedMethod, userContext );
In the preceding parameters, Paramter1, Parameter2,..., indicates the FunctionName parameter. The type is Object or Array;
SuccessMethod is the Js method that needs to be returned in the background,
FailedMethod is the Js method executed when the csFunctionName method in the background is abnormal (fault tolerance Processing Method ),
UserContext is any content that can be passed to the SuccessMethod method or FailedMethod method.


Follow these steps to implement method 3:

1. The background method must be public static,

Add [System. Web. Services. WebMethod] to the method header to mark features.

2. Add the ScriptManager control to the foreground page and set its EnablePageMethods attribute to true.

 

3. Call PageMethods, which is the simplest call.

PageMethods. FunctionName (callback js method );

// FunctionName indicates the static method name created in the background, and the callback js method indicates the method that the foreground accepts the result.


Example of PageMethods:

Background code:

I. No Parameter Method

[System. Web. Services. WebMethod] public static string ShowValue () // you can add the {return "HelloWord = C #";}

 

Front-end code:

 

<Script type = "text/javascript"> function bclick () {PageMethods. showValue (HelloWord); // call the parameter method PageMethods. showValue ('20140901', HelloWord);} function HelloWord (result) // The callback method accepts the execution result of the background code {alert (result );} // call the function bclick2 () {var text = "test"; PageMethods. showValue2 (text, sshow2) ;}</script>


Method 4: JQuery Ajax asynchronous call

A lot of Baidu

 

 

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.