Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# webBrowser.Document: reloading page after postback

I am working on a simple application that automatically browses in a page that contains two dropdown menus and a button. The page looks like this:

------DropDown1-------

------DropDown2-------

-------Button---------

Now, the problem is, the contents of DropDown2 is dynamically generated by the selection of Dropdown1.

I wrote code like this in c#:

private void webBrowser1_DocumentCompleted(object sender, 
        WebBrowserDocumentCompletedEventArgs e)
{
    HtmlElement elem = webBrowser1.Document.GetElementById("DropDown1");
    elem.SetAttribute("selectedIndex", "1");
    elem.RaiseEvent("onChange");
    HtmlElement elem = webBrowser1.Document.GetElementById("DropDown2");
    elem.SetAttribute("selectedIndex", "5");
    elem.RaiseEvent("onChange");
}

After raising the onChange event, the browser loads the new values but I can't get and set the DropDown2 value because the document still thinks DropDown2s values are empty.

How can I get and set the new values that are generated in DropDown2?

like image 420
dreampowder Avatar asked Nov 06 '22 01:11

dreampowder


1 Answers

i have found the solution by invoking "__doPostBack" script after raising onChange event. when i invove the doPostBack, the document reloads and so i can retrieve the new values. heres the code:

    private void BeginOperation()
    {
        webBrowser1.Navigate("somewebpage", false);
        Task = 0;
    }
    private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        HtmlElement elem;

        switch (Task)
        {
            case 0:
                //HtmlDocument mydoc = webBrowser1.Document;
                 elem = webBrowser1.Document.GetElementById("ddlCity");
                MessageBox.Show(elem.All.Count.ToString());
                elem.SetAttribute("selectedIndex", "1");
                //elem.RaiseEvent("onChange");
                object[] args = {"someparameters"};
                webBrowser1.Document.InvokeScript("__doPostBack",args);
                Task++;
            break;
            case 1:
                elem = webBrowser1.Document.GetElementById("ddlDistrict");
                elem.SetAttribute("selectedIndex", "2");
                elem.RaiseEvent("onChange");
                object[] args2 = {"someparameters"};
                webBrowser1.Document.InvokeScript("__doPostBack",args2);
                Task++;
            break;
        }
     }
like image 189
dreampowder Avatar answered Nov 12 '22 22:11

dreampowder