C # webBrowser.Document: reloading page after postback

I am working on a simple application that automatically views a page containing two drop-down menus and a button. The page is as follows:

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

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

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

Now the problem is that the content is DropDown2dynamically generated by the selection 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 event, the onChangebrowser loads the new values, but I cannot get and set the value DropDown2, because the document still believes that the values DropDown2are empty.

How can I get and set new values ​​generated in DropDown2?

+5
3

, "__doPostBack" script onChange. doPostBack, . heres :

    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;
        }
     }
+2

, , , , , . ....

|---> The page finishes loading, triggering your DocumentCompleted method
|---> You set the selectedIndex on DropDown1
|---> You raise the onChange event for DropDown1
|       |---> The page starts posting-back (1)
|---> You (attempt to) set the selectedIndex on DropDown2
|---> You raise the onChange event for DropDown2
|       |---> The page starts posting-back (2)
|
...
...
...
|---> The page finishes re-loading from from postback (2)

, . , / , Timer , ( , ), selectedIndex DropDown2. - :

|---> The page finishes loading, triggering your DocumentCompleted method
|---> You attach a new EventHandler to DocumentCompleted that contains the 
|     code for changing the selectedIndex on DropDown2 and REMOVE this 
|     eventhandler
|---> You set the selectedIndex on DropDown1
|---> You raise the onChange event for DropDown1
|---> Your code in the DocumentCompleted handler finishes executing


|---> // This is the DocumentCompleted handler that you assign above
|---> You set the selectedIndex on DropDown2
|---> You raise the onChange event for DropDown2
|---> Your code in the DocumentCompleted handler finishes executing

, , , .

0

Thanks for this. I searched for a solution to a similar problem for several days ... In my case, I had one drop-down menu for which the items in the list were updated during the "onchange" event. Calling __doPostBack updates WebBrowserReadyState, allowing me to wait for the "onchange" event to complete before clearing the new drop-down list values.

0
source

All Articles