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
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
ȺÀïÌÖÂÛ¹ýµÄÌâÄ¿£¬·ÖÏíһϡ£
function test(){
var m=n=1;
alert(m);
}
alert(n);
//ÒòΪtestº¯ÊýûÓÐÖ´ÐУ¬³Ì¶È¿ØÖÆÁ÷²»ÄܽøÈë½øÐнâÎö£¬ÀïÃæµÄ¶«Î÷¶ÔÍâÃæ²»¿É¼û£¬Òò´Ë±¨´í
function test(){
var m=n=1;
alert(m);
}
test();
alert(n);
//µ±testÖ´Ðкó£¬Í ......
ÔÚjavascriptº¯ÊýÌåÄÚ£¬±êʶ·ûarguments¾ßÓÐÌØÊ⺬Òå¡£ËüÊǵ÷ÓöÔÏóµÄÒ»¸öÌØÊâÊôÐÔ£¬ÓÃÀ´ÒýÓÃArguments¶ÔÏó¡£Arugments¶ÔÏó¾ÍÏñÊý×飬עÒâÕâÀïÖ»ÊÇÏñ²¢²»Êǹþ¡£
javascriptº¯ÊýÌåÄÚ£¬argumentsÏñÊý×é(²¢²»ÊÇÕæµÄÊý×飬ÊÇÒ»¸öArguments¶ÔÏó£¬ÔÙ´ÎÇ¿µ÷)Ò»Ñù£¬ÓÐlengthÊôÐÔ£¬¿ÉÒÔ´ú±í´«¸øº¯ÊýµÄ²ÎÊýµÄ¸öÊý¡£
ÒýÓÃÒ»¸öÐÎʽ²ÎÊ ......
2¡¢ÔÐÍ·½Ê½
/**
* PersonÀࣺ¶¨ÒåÒ»¸öÈË£¬ÓиöÊôÐÔname£¬ºÍÒ»¸ögetName·½·¨
*/
function Person(){}
Person.prototype.name = "jack";
Person.prototype.getName = function() { return this.name;}
°ÑÀàµÄÊôÐÔ£¨×ֶΣ©£¬·½·¨¶¼¹ÒÔÚprototypeÉÏ¡£
Ô켸¸ö¶ÔÏó²âÊÔÏ£º
var p1 = new Person();
var ......
·¢ËÍÖÐÎÄ×Ö·ûÇëÇóʱ£¬Èç¹ûʹÓÃget·½Ê½£¬ÔËÐÐÕý³££»¶øʹÓÃpost·½·¨Ôò»á³öÏÖÂÒÂë¡£ÕâÊÇÓÉÓÚÒì²½¶ÔÏóXMLHttpRequestÔÚ´¦Àí·µ»ØµÄ
responseTextµÄʱºò£¬ÊÇ°´UTF-8±àÂë½øÐнâÂëµÄ¡£Èç¹ûÄãÔÀ´µÄÍøÒ³±àÂëÊÇgb2312µÄ»°£¬µ±È»»á·¢Éú±àÂëµÄ³åÍ»ÁË£»Èç¹ûÄãÔÀ´µÄÍøÒ³±à
ÂëÊÇutf-8£¬ÄÇô¾Í²»»á³öÏÖÖÐÎÄÂÒÂëµÄÎÊÌâÁË¡£
³öÏÖÁËÖÐÎÄÂÒ ......