How to realize the Intermodulation between foreground and background methods in C #
This article shows you how to realize the intermodulation between foreground and background methods. The content is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The intermodulation between foreground and background methods is a function that many readers are concerned about. The code provided below can achieve the purpose of intermodulation between C# foreground and background methods.
< %@ Page Language="C#" AutoEventWireup="true" CodeFile="JavaScript get Net Function.aspx.cs" Inherits="JavaScript_get_Net_Function" %> < !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head runat="server"> < title>Summary of foreground and background method protection and adjustment
< /title> < %-- JavaScript异步调用定义在ASP.Net页面中的方法, 我们需要: 1.将该方法声明为公有(public); 2.将该方法声明为类方法(C#中的static,VB.NET中的Shared),而不是实例方法; 3.将该方法添加【WebMethod】属性 4.将页面中ScriptManager控件的EnablePageMethods属性设置为true; 5.在客户端使用如下JavaScript语法调用该页面方法 PageMethods.[MethodName](param1,param2,...,callbackFunction); 6.为客户端异步调用指定回调函数,在回调函数中接受返回值并进一步处理; 7.添加 using System.Web.Services; --%> < %-- ASP.Net后台调用JavaScript function的方法,我们要注意: 1.尽量少用Response.Write(< script> < /script>), it will affect CSS to cause page effect deviation in 2.C# using ScriptManager.RegisterStartupScript (this, this.GetType (), "edit", "JS ('" + param1+ ",'" + param2+ ")", true); 3. Add using System.Web.UI;--% >
< script type="text/javascript">/ *-JavaScript calls .net background Start--*/ function JsCallCSharp (param1) {PageMethods.SayHelloFromPage (param1,onSayHelloSucceeded);} function onSayHelloSucceeded (result) {alert (result) } / *-JavaScript calls .net background End--*/ / *-.net calls JavaScript Start-- -* / function CSharpCallJs (param1 Param2) {alert (param1 + param2) } / *-.net calls JavaScript Start--*/
< /script> < /head> < body> < form id="form1" runat="server"> < div> < asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" /> < /div> < asp:Button ID="Button1" onclick=Button1_Click" OnClientClick="JsCallCSharp('takako');" runat="server" Text="Button1" /> < /form> < /body> < /html>C # code of intermodulation between foreground and background methods
Using System; using System.Web.Services; using System.Web.UI; public partial class JavaScript_get_Net_Function: System.Web.UI.Page {protected void Page_Load (object sender, EventArgs e) {} protected void Button1_Click (object sender, EventArgs e) {string param1 = "Hello:"; string param2 = "takako_mu" ScriptManager.RegisterStartupScript (this, this.GetType (), "edit", "CSharpCallJs ('" + param1 + "','" + param2 + "');", true);} [WebMethod] public static string SayHelloFromPage (string name) {return string.Format ("Hello {0}!", name) }} the above content is how to realize the interadjustment between foreground and background methods in C #. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.