form1向form2里边的webBrowser传值(form2.cs中有webBrowser控件,显示的是table.html网页)
程序员文章站
2022-06-07 13:22:11
...
form1.cs,一个button按钮
public static string time1;
private void button_Click(object sender,EventArgs e)
{
time1=DateTime.Now.ToString("yyyy.MM.dd").Trim();
Form2 form2 = new Form2();
form2.ShowDialog();
}
form2.cs,一个button按钮,一个webBrowser控件
在窗口添加网页内容
private void Form2_Load(object sender,EventArgs e)
{
webBrowser1.Navigate("E:/table.html");//显示网页
// webBrowser1.ObjectForScripting = this;
}
点击按钮,(在网页中显示form1传来的值)
private void button_Click(object sender,EventArgs e)
{
string time = Form1.time1;
webBrowser1.Document.InvokeScript("write",new string[],{time});
//webBrowser1.Document.InvokeScript("write",new string[],{label1.Text});
}
table.html
<html>
<head>
<script type="text/javascript">
function write(str)
{
document.getElementById("target").value = str;
}
</script>
</head>
<body>
<input type="text" id="target" />
</body>
</html>