前台页面:WebForm2.aspx
<!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></title>
<script language="javascript" type="text/javascript">
//获取WinForm穿过来的值
function getWinFormValue(winFormValue) {
document.getElementById("spWinFormValue").innerHTML = winFormValue;
}
//调用WinForm里的方法将值传过去
function SetWinFormValue(WebFormValue) {
//后台页面的方法 Call
window.external.Call(WebFormValue);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
WinForm传过来的值:<span id="spWinFormValue"></span>
<br />
请输入值:<asp:TextBox ID="txtValue" runat="server"></asp:TextBox>
<asp:Button ID="btn" runat="server" Text="点击我将值传入WinForm" OnClientClick="var v=document.getElementById('txtValue').value;alert(v);SetWinFormValue(v);return false;"
Width="142px" onclick="btn_Click" />
</div>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------------------
Form窗体后台代码:Form2.cs
namespace WindowsFormsApp
{
//申名托管类型,对com是可见的
[System.Runtime.InteropServices.ComVisible(true)]
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.wbWeb.Navigate(string.Format("javascript:getWinFormValue('{0}');void(0);", this.textBox1.Text));
}
//前台js调用的方法
public void Call(string WebFormValue)
{
this.label1.Text = WebFormValue;
}
private void Form2_Load(object sender, EventArgs e)
{
//此窗体可以通过wbweb控件里面的脚本进行访问
wbWeb.ObjectForScripting = this;
}
}
}
此方法要比前文的 使用webservice实现要简便性能要好!