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 "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Headrunat= "Server"> <title></title> <styletype= "Text/css">#txtValue{Height:63px;width:543px; }#Button1{Height:33px;width:541px; }#btnTest{width:539px; } </style> <Scriptlanguage= "JavaScript"type= "Text/javascript"src= "Easyui/jquery-1.8.0.min.js"></Script> <Scriptlanguage= "JavaScript"type= "Text/javascript">$ (document). Ready (function () { $("#btnTest"). Bind ("Click", function() {window.external.CheckUser (); }); }); functionCallbackfun (e) {$ ("#txtValue"). Val (e); } </Script></Head><Body> <inputID= "Txtvalue"type= "text" /> <BR/> <BR/> <inputID= "Btntest"type= "button"value= "Go" /></Body></HTML>
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:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Security.Permissions;namespacewindowsformsapplication1{[PermissionSet (SecurityAction.Demand, Name="FullTrust")] [System.Runtime.InteropServices.ComVisibleAttribute (true)] Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } PublicWebBrowser Browser {Get{return This. WebBrowser1;} } Private voidForm1_Load (Objectsender, EventArgs e) {webbrowser1.objectforscripting= This; WebBrowser1.Navigate ("http://localhost:8008/default.aspx"); } Public voidCheckUser () {WindowsFormsApplication1.Comm.Global.Main= This; BOOLresult=true; /** This allows for arbitrary logic, and finally assigns the result to results * **/ if(Result)//Judging Results{WindowsFormsApplication1.Comm.Global.Main.Invoke (NewMethodInvoker (Delegate() {WindowsFormsApplication1.Comm.Global.Main.Browser.Document.InvokeScript ("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!!
Understanding JS callback function