Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of interaction between javascript and background c # in asp.net

Shulou Source: shulou.com Published: 2022-06-03 19:11:29 11月12日 Update

This article mainly shows you the "example analysis of the interaction between javascript and background c # in asp.net". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "example analysis of the interaction between javascript and background c # in asp.net".

There are four main aspects in the intercall between C # code and javaScript function:

1. How do I access C # functions in JavaScript?

two。 How do I access C# variables in JavaScript?

3. How to access the existing variables of JavaScript in C #?

4. How to access the JavaScript function in C #?

1. Execute the function in the C# code in the javaScript function:

Method 1: combine page with page class

1. The function is declared as public

Background code (you can also change public to protected)

Public string ss () {return ("a");}

2. Use the name of the function that can be called / / in the background of C# in html

Foreground script

Var a = ""; / / call the C# background function alert (a) in JavaScript

Method 2: JavaScript asynchronously calls the method defined in the ASP.Net page

1. Declare the method as public

two。 Declare the method as a class method (Shared in static,VB.NET in C #), not an instance method

3. Add the method to the [WebMethod] attribute

4. Set the EnablePageMethods property of the ScriptManager control on the page to true

5. Call the page method on the client using the following JavaScript syntax

PageMethods. [MethodName] (param1,param2,...,callbackFunction)

6. Specify the callback function for the client asynchronous call, accept the return value in the callback function and further process

7. Add using System.Web.Services

Example:

Foreground JavaScript code

Untitled page function JsCallCSharp (param1) {PageMethods.sayhell (param1,onSayHelloSucceeded); / / sayhell is the method marked with the [webMethod] attribute in the background param1 is the parameter passed into the method, onSayHelloSucceeded is the callback function, which is mainly used to further deal with the results returned by the background} function onSayHelloSucceeded (result) / / bound callback function {alert (result);} / / ScriptManager control manages scripts, pay attention to setting EnablePageMethods= "true" this property

Tags: Methods functions backends services code pages clips variables properties scripts foreground controls examples caches analysis content name client client article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MariaDB vpn Microsoft OPPO Reno