C#中使用Webbrowser控件如何传值
来源:互联网 发布:软件著作权包括哪些 编辑:程序博客网 时间:2024/05/18 08:00
在使用Winform开发时,需要用到Webbrowser控件用来展示页面,如何将控件的值传给页面呢?
一. 对于少量参数值,可以在url地址中加入需要传输的值。前台页面js解析url地址参数即可
//C#代码,传输用户ID为80的值给testPage.aspx页面string url = "localhost:8080//testPage.aspx?userID="+80;webBrowser1.Navigate(url);
//JS代码,读取url中的userIDuserID = GetQueryString("userID");//获取地址栏中的用户IDfunction GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return decodeURI(r[2]); return null;}
二. 对于大量数据,比如后台获取的大量数据在页面中用图展现,可以使用webBrowser的ObjectForScripting属性。
MSDN上对ObjectForScripting的解释是:获取或设置一个对象,该对象可由显示在 WebBrowser 控件中的网页所包含的脚本代码访问,一开始看不懂这句话什么意思,仔细琢磨,其实就是说webBrowser控件指向的页面可以通过页面的js访问ObjectForScripting设置的对象,该对象一般设置为后台对象,这样就可以使前台页面访问后台方法。
在设置了webBrowser控件的ObjectForScripting属性后,还需要设置应用程序对com可见,不然会抛出一个异常 ,可做如下设置: [System.Runtime.InteropServices.ComVisible(true)]
//C#调用窗口BusinessDataExportFigure bdef = new BusinessDataExportFigure();bdef.strData = resultStr;//结果数据赋值传递bdef.Show();
//C#后台代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace sample{ [System.Runtime.InteropServices.ComVisibleAttribute(true)]//标记对com可见 public partial class BusinessDataExportFigure : Form { public string strData;//用于承接后台数据 public BusinessDataExportFigure() { InitializeComponent(); } public string GetData() { //供页面调用以传递数据 return strData; } private void BusinessDataExportFigure_Load(object sender, EventArgs e) { this.webBrowser1.ObjectForScripting = this;//设置对象为当前BusinessDataExportFigure窗体 string url = "localhost:8080//testPage.aspx"; this.webBrowser1.Navigate(url); } }}
上面代码是在一个名称命名为BusinessDataExportFigure的Form中添加了一个webBrowser控件,该webBrowser控件指向testPage.aspx页面
//testPage页面<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title></head><body> <div> <button onclick="showData()">点击查看</button> </div></body> <script> function showData() { var data = window.external.GetData();//使用window.external直接调用后台C#方法 alert(data); } </script></html>
题外话:上面实现了JS调用后台C#方法,那么C#是否也可以调用页面JS函数呢?
这个是可以的,使用webBrowser控件的Document对象的InvokeScript方法即可。
该方法有两种参数形式
- InvokeScript(string):string为要执行的脚本函数的名称,JS函数不需要参数
- InvokeScript(string,Object[]):string为要执行的脚本函数的名称;Object[]要传递到脚本函数的参数
例如:
//HTML代码<script type="text/javascript"> function sayHello(name,message) { alert(name+","+message); } </script>
调用:
//C#if(this.loadCompleted)//在文档加载完毕后调用{ webBrowser1.Document.InvokeScript("sayHello", new Object[] {Tom,"hello"}); }
需要注意的一点是,使用InvokeScript方法需要在文档加载完毕后,所以需要检查文档加载情况
- C#中使用Webbrowser控件如何传值
- C#中使用WebBrowser控件
- c#中WebBrowser控件
- c#中WebBrowser控件 .
- c#中WebBrowser控件
- 如何使用 WebBrowser 控件在 Visual C# 2005 或 Visual C# .NET 中打开 Office 文档
- 如何使用 WebBrowser 控件在 Visual C# 2005 或 Visual C# .NET 中打开 Office 文档
- 如何使用 WebBrowser 控件在 Visual C# 2005 或 Visual C# .NET 中打开 Office 文档
- 如何使用 WebBrowser 控件在 Visual C# 2005 或 Visual C# .NET 中打开 Office 文档
- C# webBrowser控件使用心得
- C# webBrowser控件使用心得
- C# webBrowser控件使用心得
- C# webBrowser控件使用心得
- C# webBrowser控件使用心得
- c#多线程使用webbrowser控件
- C# WebBrowser 控件的使用
- C# webBrowser控件使用心得
- 在Qt中如何使用WebBrowser这个ActiveX控件
- Ubuntu rz,sz安装
- Linux: SecureCRT 入门
- mysql 数据库常用命令
- 【LeetCode】C# 78、Subsets
- 正则表达式不再难懂
- C#中使用Webbrowser控件如何传值
- WPS简单五步操作修改图形并调整图形位置大小和旋转角度
- jeesite.properties配置说明
- LUA版 四阶幻方生成办法
- Ubuntu-apt-get保存路径
- 华为上机题汇总
- 楼层扔鸡蛋 ? 待解决
- sizeof&strlen
- linux下源码安装tree命令