0

I am trying to manipulate html of a url in WinForms for automation purpose, On the webpage there is an anchor tag:

<Ahref="javascript:__doPostBack('dgBloodDonorResults$ctl01$ctl01','')">2</A>

How can I fire __dopostback() automatically??

I tried this:

           mshtml.HTMLDocument doc = (mshtml.HTMLDocument)webBrowser1.Document.DomDocument;

            mshtml.IHTMLElementCollection anchors = doc.getElementsByTagName("a");

            foreach (mshtml.IHTMLElement anchorElement in anchors)
            {
                mshtml.HTMLAnchorElement anchor = anchorElement as mshtml.HTMLAnchorElement;
                if (anchor != null)
                {
                    string outerHTML = anchor.outerHTML;

                    if (outerHTML.Contains("dgBloodDonorResults$"))
                    {
                      
                        if (currentGridPage +1 <= totalPagesInGrid)
                        {
                            currentGridPage++;
                            System.Diagnostics.Debug.WriteLine(anchor.href + " HTML: " + outerHTML);

                            anchor.click(); //I Want this to firing.

                          
                        }
                        else
                        {
                            isPostback = false;
                            currentGridPage = 0;
                            return;
                        }
                        //UpdateBrowser();
                    }
                }
            }
           `
Vizel Leonid
  • 456
  • 7
  • 15
Saurabh Sashank
  • 280
  • 1
  • 5
  • 18

1 Answers1

0

You can directly invoke the method:

webBrowser1.Document.InvokeScript("__doPostBack", 
      new object [] { "dgBloodDonorResults$ctl01$ctl01", "" } 
    );

Documentation on MSDN

Jcl
  • 27,696
  • 5
  • 61
  • 92
  • I tried this but page is not posting back, i also tried 'code' object[] args = { "dgBloodDonorResults$ctl01$ctl02", "" }; webBrowser1.Document.InvokeScript("__doPostBack", args);' – Saurabh Sashank Jan 20 '15 at 14:20
  • Make sure the document is loaded fully before attempting to invoke – Jcl Jan 20 '15 at 14:21
  • Yes i am invoking script after document is fully loaded, but then also is not firing what could be the issue. – Saurabh Sashank Jan 20 '15 at 14:26
  • I don't know. I have used that code successfully many times to call a javascript method on a webbrowser control. There must be something else wrong. – Jcl Jan 20 '15 at 15:49
  • I am also using this code for other javascript functions but only __dopostback() method is not firing – Saurabh Sashank Jan 21 '15 at 08:09
  • You could try to add a javascript function that calls `__doPostBack` with an `alert` first and see if the problem is the invoking or the actual `__doPostBack`. Something like `` and invoke that instead of `__doPostBack` directly; – Jcl Jan 21 '15 at 08:11
  • yes, tried this approach alert is coming but actual postback is not happening. – Saurabh Sashank Jan 21 '15 at 13:17
  • So it seems `__doPostBack` is somehow not defined or raising some error since the invoking of the method is right. Does it work when you click the anchor text? If so, has that anchor have any other click events attached somehow? If the invoking of the javascript is right (and it is, since you are seeing the alert), then it's no more a `WebControl` problem and you should debug the actual web page or javascript. – Jcl Jan 21 '15 at 13:20