We often use the client-to-Web project in conjunction with the development requirements, so this involves executing the foreground dynamic script function on the client, which is called the function callback, this article illustrates the process of the callback function.
First create a Web project, a very simple page, only a button and a textbox, the code and Effect are as follows:
<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Webtest.default"%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
CheckUser: Represents a method for the client.
Callbackfun: A function that represents a client callback.
The CheckUser method of the client is executed in the Btntest click event, when the client callbacks the Callbackfun function, the text box with ID Txtvalue is assigned, and the assignment content is the content returned from the callback function.
Create a client program below, the interface is very simple, there is only one WebBrowser1 button, the code and interface are as follows:
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;using system.security.permissions;namespace windowsformsapplication1{[PermissionSet (SecurityAction.Demand, Name = "FullTrust")] [System.Runtime.InteropService S.comvisibleattribute (true)] public partial class Form1:form {public Form1 () {Initializ Ecomponent (); } public WebBrowser Browser {get {return this.webbrowser1;}} private void Form1_Load (object sender, EventArgs e) {webbrowser1.objectforscripting = this; WebBrowser1.Navigate ("http://localhost:8008/default.aspx"); } public void CheckUser () {WindowsFormsApplication1.Comm.Global.Main = this; BOOL Result=true; /* * This allows for arbitrary logic and finally assigns results to result * */ if (result)//judgment result {WindowsFormsApplication1.Comm.Global.Main.Invoke (new Methodinvo Ker (delegate () {WindowsFormsApplication1.Comm.Global.Main.Browser.Document.InvokeScrip T ("Callbackfun", new object[] {"777"}); })); } } }}
The WebBrowser1 URL (http://localhost:8008/default.aspx) is the IIS site of my local computer.
We use the WebBrowser1 Invokescript method to perform the callback function Callbackfun, which returns an object that returns a "777" string.
After running, click the button, the effect is as follows:
As you can see, the client has successfully recalled the function Callbackfun and has passed the client's return value "777" to the foreground.
If you feel useful, please help to top it, thank you!!
JavaScript function Callback