我在Windows Form中用axWebBrowser控件来操作网页,
在获取用户点击按钮事件,已经成功,
但是同时又出现了新的问题,
如果那个按钮原来用单击事件:
<input type="button" class="b_foot" value="订单提交" id="nextBtn"  onclick="doCommit()"/>我在程序中使用代码:
onclickHandler = new BrowserEventHandler();
onclickHandler.Fire = new ElementEventFire(OnClickButton);
photo.onclick = onclickHandler;
来获取用户单击事件,这个单击事件获取到了,但是网页中原来的
onclick="doCommit()"事件却没有了反应,
我如何在获取点击事件,而又不影响原本页面中的动作那。
请高手赐教。

解决方案 »

  1.   


      //这样处理,先找到按钮对象
     HtmlElement btnTarget= webBrowser1.Document.GetElementById("btnTarget");            if (btnTarget== null )
                {
                    return;
                }
                btnTarget.InvokeMember("click");
      

  2.   

    上面就是直接找到按钮对象,然后click它,而不是给它挂接事件,能避免你的问题!
      

  3.   

     HtmlElement bt = this.webBrowser1.Document.GetElementById("submit"); 
                if (bt != null) 
                { 
                    backgroundWorker1.RunWorkerAsync("submit");
                } 
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                HtmlElement btn= this.webBrowser1.Document.GetElementById("submit");
                btn.InvokeMember("click"); 
            }
    webBrowser1.Document.GetElementById("").InvokeMember("Click");
      

  4.   

    回楼上,我用的axWebBrowser ,并且在iframe的嵌套里,好像没有那个 HtmlElement 和InvokeMember,
      

  5.   

    在js中写,给你个简单例子<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <TITLE> New Document </TITLE>
    <script type="text/javascript">
            
            function fun1(){
                 var aa = document.getElementById("aaa");
                 aa.innerHTML = "---";
                 aa.onclick = function(){fun2();}
        
            }
        
            function fun2(){
                var aa = document.getElementById("aaa");
                aa.innerHTML = "+++";
                aa.onclick = function(){fun1();}
             }    </script>
     </HEAD> <BODY>
    <table width="100%" border="0" cellspacing="2">
           <tr>
               <td width="100%" height="25" bgcolor="#33BB33">
                    <P>
                    <a id="aaa" style="cursor:hand" onClick="fun1()">+++</a>
                    </P>
               </td>
           </tr>
    </table> </BODY>
    </HTML>
      

  6.   


    你的意思找不到代表按钮的HtmlElement ?
      

  7.   

    在axWebBrowser 没有那个属性,有DispHTMLInputElement这样的,但是找不到单击事件,axWebBrowser跟WebBrowser 的封装不一样
    你有代码吗?一楼的代码也是按你以前的代码改的。
      

  8.   

    http://topic.csdn.net/u/20100112/21/e77866ba-d764-4a9e-804b-139c6eea50e4.html
      

  9.   

    在axWebBrowser 没有那个属性,有DispHTMLInputElement这样的,但是找不到单击事件,axWebBrowser跟WebBrowser 的封装不一样
    你有代码吗?一楼的代码也是按你以前的代码改的。

    你用WebBrowser吧!
      

  10.   

    我以前也是用的WebBrowser,但是在打开新页面时有问题。我的程序必须也能控制新页面,就是说新页面也要在我程序代开,在页面里有
    window.open(' dmain.htm','CRM','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=500,height=500,left=0,top=0');
    window.opener=null;
    window.open("","_self");
    window.close();
    时无效,现在我只在axWebBrowser 解决了那个问题
      

  11.   

    回楼上:
    可能我说的不太明白,让大家有些误解:
    我程序用的axWebBrowser控件,用IHTMLDocument2获取多层iframe里的代码。
    mshtml.IHTMLDocument2 idc = frm.document;
    在IHTMLDocument2好像没有InvokeMember这个,并且我要的不是自动触发事件,而是当用户点击一个按钮时获得他点击的动作,就是说我要知道他点了比如提交按钮,这样我可以在程序中加一些操作,但是那个提交按钮原本有onclick事件,我程序在扑捉他点击动作的同时不能影响按钮原本的onclick事件。
      

  12.   

    难道你的一个事件都多个onclick。
      

  13.   

    把原来的onclick的事件处理函数存下来在你的onclick里面调用。
    调用方法参考InvokeMember的源代码
      

  14.   


    这个问题好解决呀!
    WebBrowser 控件应用:弹出新窗体和关闭窗口
      

  15.   

    WebBrowser 控件应用:弹出新窗体和关闭窗口这篇文章我已经看过,按上面上的解决不了问题,包括360多标签浏览器浏览时都会提示:禁止非法进入页面,
    因为新页面里有类似这样的验证。<script language="javascript">
    if(typeof(opener) == "undefined")
    {
      alert("禁止非法进入页面!");
      window.opener=null;
      window.open("","_self");
                     window.close();
             }
    </script>因为我要处理的页面比较复扎,是移动公司的一个业务系统,对浏览器做了很多的限制。下面是我处理页面的一部分代码,请看指教一下如何修改能解决问题:
    private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
            {
                if (axWebBrowser1.ReadyState == SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                {
                    mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)axWebBrowser1.Document;
                    mshtml.IHTMLWindow2 frm = (mshtml.IHTMLWindow2)doc.parentWindow;
                    mshtml.FramesCollection frms = frm.frames;
                    object x;
                    for (int i = 0; i < frms.length; i++)
                    {
                        x = i;
                        mshtml.IHTMLWindow2 ifrme = (mshtml.IHTMLWindow2)frms.item(ref x);
                        if (ifrme != null) { getiframe(ifrme); }
                    }
                }
            }private void getiframe(mshtml.IHTMLWindow2 frm)
            {
                mshtml.IHTMLDocument2 idc = frm.document;
                mshtml.IHTMLElementCollection hec = ((mshtml.IHTMLDocument3)idc).getElementsByTagName("input");
                 mshtml.DispHTMLInputElement photo = null;
                foreach (object obj2 in hec)
                {
                    photo = obj2 as mshtml.DispHTMLInputElement;
                    if (photo.id == "btnSave" && photo.value == "交费")
                    {
                        onclickHandler = new BrowserEventHandler();
                        onclickHandler.Fire = new ElementEventFire(OnClickButton);
                        photo.onclick = onclickHandler;
                    }
                }            mshtml.IHTMLWindow2 idc_parentwindow = (mshtml.IHTMLWindow2)idc.parentWindow;
                mshtml.FramesCollection myfrm = idc_parentwindow.frames;
                int idc_parentwindow_count = myfrm.length;
                if (idc_parentwindow_count > 0)
                {
                    object x;
                    for (int i = 0; i < idc_parentwindow_count; i++)
                    {
                        x = i;
                        mshtml.IHTMLWindow2 ifm = (mshtml.IHTMLWindow2)myfrm.item(ref x);
                        if (ifm != null) { getiframe(ifm); }
                    }
                }
            }
      

  16.   

    我帮你解决了,因为你查找下一页的方法不对,不能用这样的方法:                foreach (HtmlElement link in links)
                    {
                        if (link.InnerText != null && link.InnerText.Contains("下一页"))
                        {
                            usefullink = link;
                            break;
                        }
                    }
    应该这样!        private void button1_Click(object sender, EventArgs e)
            {
                HtmlElement btnSearch = webBrowser1.Document.GetElementById("btnG");
                HtmlElement txtContact = webBrowser1.Document.GetElementById("q");
                if (txtContact == null || btnSearch == null)
                {
                    return;
                }
                txtContact.SetAttribute("value", "中学");
                btnSearch.InvokeMember("click");            HtmlElementCollection links = this.webBrowser1.Document.Links;
                bool a = GoTargetPage(links);
                //如果没有找到,就去下一页找
                if (a == false)
                {
                    foreach (HtmlElement link in links)
                    {
                        if (link.InnerText == "下一页")
                        {
                            link.InvokeMember("click");
                        }
                    }
                }
    }