Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

VB.NET/C# and JavaScript communication

Introduction
This article is about passing data between VB.NET/C# WinForms and JavaScript.
Before reading further, let me warn you that this article is not about ASP.NET. Concepts covered in the article are applied to Desktop applications.
Background
I was working on a project which required data transfer between my VB.NET WinForms application and the JavaScript (inside an HTML page). Along the way, I hit certain problems, and trying to resolve them cost plenty of time on the web (on Google mostly), so I thought of this article as a platform for developers looking to sort out similar issues. There isn't much detail on this topic on the web apart from a couple of Hello World examples from Microsoft on MSDN.
Starting point
OK, without any further talk, I will dig in to the subject.
Hello World
To start off, we'll start with a very simple example; all this will do is call a JavaScript function from VB.NET to display an alert with message 'Hello world'. Similarly, from the HTML page using JavaScript, we'll call a VB.NET function which will again display a messagebox with the message 'Hello world'. These are the steps you need to do to make it happen:
Calling JavaScript from VB.NET
In Visual Studio, create a new WinForms application, name it anything you like.
Add an import statement like Imports System.Security.Permissions in your form1 (main form).
Add a couple of attributes to form1, like:
Collapse Copy Code
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
End Class
All they do is tell the .NET Framework that we want fulltrust and make the class visible to COM so this class is visible to JavaScript.
Add a WebBrowser control to the form and set its url property to c:\temp\mypage.html (just an example path, you should set it to the HTML page you'll be using).
Add a Button control on the form, and on its click handler, write this code:
Collapse


Ïà¹ØÎĵµ£º

Javascriptµ¯³ö¶Ô»°¿ò3ÖÖ·½Ê½

<!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
 &nb ......

asp.net×¢²áJavascriptµÄ·½·¨

1¡¢IsStartupScriptRegistered£º¶ÔÏóÊÇ·ñ×¢²áÁËÆôÓýű¾¡£
2¡¢IsClientScriptBlockRegistered£º¶ÔÏóÊÇ·ñ×¢²áÁË¿Í»§¶Ë½Å±¾¿é¡£
3¡¢IsClientScriptIncludeRegistered£º¶ÔÏóÊÇ·ñ×¢²áÁË¿Í»§¶Ë½Å±¾°üº¬¡£
µÚÒ»¸öÀý×Ó£º
string info = "<script>alert('ÄãºÃÂð£¿');</script>";
//ÅжÏʼþÊÇ·ñ±»×¢²á£¬Ð´ÈëÇ ......

VBµ÷ÓÃVC DLL Ô¼¶¨´íÎó,Çó¸ßÊÖÖ¸µã

VCº¯ÊýÊÇ:extern "C" int __declspec(dllexport)PassPortRead(char *InPutData,char OuPutData[255]);
VBÉùÃ÷ÊÇ£ºPrivate Declare Function PassPortRead Lib "PPRead.dll" (ByVal InPutData As String, ByVal OutPutData As String) As Integer
Éú³ÉµÄÎļþÄÜÕý³£ÔËÐÐ,²¢ÇÒÄÜÉú³ÉÏàÓ¦Êý¾Ý,µ«ÊÇÔÚµ÷ÊÔʱÌáʾ"DLLµ÷ÓÃÔ¼¶¨´í ......

vbʵÏַ½ðɽ´Ê°ÔµÄÆÁĻȡ´Ê¹¦ÄÜ

½ñÌìÏëÒª×öÀàËƽðɽ´Ê°ÔÆÁĻȡ´ÊµÄ¹¦ÄÜ£¬ÔÚÍøÉÏÕÒÁËһЩ×ÊÁÏ£¬·¢ÏÖ¶¼ºÜ¸´ÔÓ¡£¼ÈÈ»½ðɽ´Ê°ÔÒѾ­Êµ
ÏÖÁË£¬½èÓÃÖ®:
½ðɽ´Ê°ÔÖÐ2005ÖдøÁËÒ»¸öXdictGrb.dll£¬ÆäʵÊÇÒ»¸öCOM×é¼þ£¬²¢ÇÒʵÏÖÁËÒ»¸öÈ¡´ÊµÄ²å¼þ¿ò¼Ü¡£
ÆäÖеÄÓÐÒÔÏÂÄÚÈÝ£º
1 ½Ó¿Ú
1.1 GrabProxy È¡´Ê´úÀí¶ÔÏó
Function AdviseGrab(XDictGrabSink As IXDictGra ......

JavaScript ÖÐÎÄÂÒÂë½â¾ö·½·¨

·¢ËÍÖÐÎÄ×Ö·ûÇëÇóʱ£¬Èç¹ûʹÓÃget·½Ê½£¬ÔËÐÐÕý³££»¶øʹÓÃpost·½·¨Ôò»á³öÏÖÂÒÂë¡£ÕâÊÇÓÉÓÚÒì²½¶ÔÏóXMLHttpRequestÔÚ´¦Àí·µ»ØµÄ
responseTextµÄʱºò£¬ÊÇ°´UTF-8±àÂë½øÐнâÂëµÄ¡£Èç¹ûÄãÔ­À´µÄÍøÒ³±àÂëÊÇgb2312µÄ»°£¬µ±È»»á·¢Éú±àÂëµÄ³åÍ»ÁË£»Èç¹ûÄãÔ­À´µÄÍøÒ³±à
ÂëÊÇutf-8£¬ÄÇô¾Í²»»á³öÏÖÖÐÎÄÂÒÂëµÄÎÊÌâÁË¡£
³öÏÖÁËÖÐÎÄÂÒ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ