日期:2014-05-18  浏览次数:20770 次

在线程或者Timer中如何调用js中的函数
winfrom中用webbrowser嵌入html网页
在正常情况下调用是没有问题的 
 
  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  private void button2_Click(object sender, EventArgs e)
  {
  bmapBrowser.Document.InvokeScript("showMarker", new object[] { num });
  }

但是在线程中调用会报错
  System.Timers.Timer t = new System.Timers.Timer(1000);

  t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
  t.AutoReset = true;
  t.Enabled = true;

  [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
  [System.Runtime.InteropServices.ComVisibleAttribute(true)]
  private void theout(object source, System.Timers.ElapsedEventArgs e)
  {
  bmapBrowser.Document.InvokeScript("showMarker", new object[] { num });
  } 

我试着调用了以下不带参数的方法 如bmapBrowser.Document.InvokeScript("clear"); 一样会报错 初步判断在线程中不可调用js的函数 求解决方法 拜谢

------解决方案--------------------
js:

<script type="text/javascript">

function ss() {

alert("底层调用js,调用成功!");

}

//这个方法用了调用隐藏按钮

function xx() {

document.getElementById("Button2").click();

}

</script>

html:


   <asp:Button ID="Button1" runat="server" Text="底层调用js" onclick="Button1_Click" />


<hr />


<input type="button" value="js调用底层" onclick="xx();" />


<asp:Button ID="Button2" runat="server" Text="我是隐藏按钮" onclick="Button2_Click" style="display:none;"/>


<hr />


CS:



//底层调用js


protected void Button1_Click(object sender, EventArgs e)


{


Page.ClientScript.RegisterStartupScript(ClientScript.GetType(), "gg", "<script>ss(); </script>");


}





//后台方法


public void showval()


{


Response.Write("js调用后台方法成功!");


}




//隐藏按钮事件


protected void Button2_Click(object sender, EventArgs e)


{


showval();


}