2011-11-25 128 views
3

我想调用C#中的onclick。我把一个WebBrowser到基于C#的程序,并在网站上,有一个js功能从C调用onclick方法#

type="button" 
onclick="submitEdgeStory();" 
value="Invia la news" class="submit" 

我想打电话给onclick方法绑一个按钮。我试着打电话给submitEdgeStory有:

webBrowser1.Document.InvokeScript("submitEdgeStory()"); 

webBrowser1.Document.Forms[1] 
        .SetAttribute("onclick", "return submitEdgeStory()"); 

但没办法..我不知道我怎么可以调用这个函数...

PS:我不使用ASP.NET 。这只是一个桌面应用程序

而且也这是js函数

var gPageIsOkToExit = false; 

    function submitEdgeStory() 
    { 
     // Set a variable so that our "before unload" exit handler knows not to verify 
     // the page exit operation. 
     gPageIsOkToExit = true; 

     // Do the submission. 
     // var frm = document.getElementById("thisform"); 
     frms = document.getElementsByName("ATISUBMIT"); 

     if (frms) 
     { 
      if (frms[0]) 
       frms[0].submit(); 
     } 
    } 

    window.onbeforeunload = function (event) 
    { 
     // See if this is a safe exit. 
     if (gPageIsOkToExit) 
      return; 

     if (!event && window.event) 
       event = window.event; 

     event.returnValue = "You have not hit the Submit Button to submit your story yet."; 
    } 
+0

? WinForms之一,WPF之一,还是Silverlight之一? –

+0

我使用winform .. – ertan2002

回答

1

我解决它像你使用的是哪个web浏览器这样

 foreach (HtmlElement item in webBrowser1.Document.All) 
     { 
      if (item.OuterHtml != null) 
      { 
       if (item.OuterHtml.Contains("submitEdgeStory")) 
       { 

        item.InvokeMember("click"); 
        break; 
       } 
      } 
     } 
1

如果您分配一个ID,您的按钮

type="button" 
onclick="submitEdgeStory();" 
value="Invia la news" class="submit" id="buttonid" 

,那么你可以做 -

WebBrowser1.Document.GetElementById("buttonid").InvokeMember("submit") 
+0

当我做到了,我得到了这个消息http://img714.imageshack.us/img714/1476/errc.png – ertan2002

+0

尝试 - 'webBrowser1.Document.Forms [0] .InvokeMember(“Submit “)',这是否给出了相同的结果? – ipr101

+0

是一样的结果:S – ertan2002

0

webBrowser.Document.Forms[0].All.GetElementsByName("buttonname")[0].InvokeMember("click");应该工作正常。

+0

heyy thanx它的作品:)但我不知道按钮名称,然后我用[代码]列表 hl =新列表(); string s =“”; foreach(webBrowser1.Document.All中的HtmlElement项) hl.Add(item); s = item.GetAttribute(“value”); (“==”Invia la news“) { item.InvokeMember(”click“); 休息; } } [/ code]但是s ==“Invia la news”不适合编码.. – ertan2002