Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RaiseEvent("onchange")

I have a winform and a WebBrowser control and I am changing an option in select HTML control.

webBrowser1.Document
    .GetElementsByTagName("select")[4]
    .GetElementsByTagName("option")[13]
    .SetAttribute("selected", "true");

Now it works and selects the required option, but it does not fire the onchange event. The select does not have an element id but it does have a class name.

I tried:

webBrowser1.Document
    .GetElementsByTagName("select")[4]
    .RaiseEvent("onchange");

and

webBrowser1.Document
    .GetElementsByTagName("select")[4]
    .GetElementsByTagName("option")[13]
    .RaiseEvent("onchange");

But in vain.

like image 586
Ismail Avatar asked May 18 '11 12:05

Ismail


3 Answers

I tried and sent a TAB key after selecting an option and it raised the onchange event.

    webBrowser1.Document.GetElementsByTagName("select")[4].Focus();
    webBrowser1.Document.GetElementsByTagName("select")[4]
.GetElementsByTagName("option")[13].SetAttribute("selected", "true");
    SendKeys.Send("{TAB}");

Everything is good now.

like image 196
Ismail Avatar answered Nov 02 '22 22:11

Ismail


try raising onChange event: e.g. RaiseEvent("onChange");

EDIT: it will be on the select element, not the option.

EDIT2:

var selectControlElement = webBrowser1.Document
    .GetElementsByTagName("select")[4];
selectControlElement.RaiseEvent("onChange");

Also its worth inspecting selectControlElement to see if it has any events.

Another option (untested by me)

object obj = selectControlElement.DomElement;
System.Reflection.MethodInfo mi = obj.GetType().GetMethod("onchange");
mi.Invoke(obj, new object[0]);

Also try

selectControlElement.InvokeMember("onchange");
like image 2
Michal Ciechan Avatar answered Nov 02 '22 23:11

Michal Ciechan


Please see the code below for details.

    void SetComboItem(string id, string value)
    {

        Forms.HtmlElement ddRouteSelected = wBrowser.Document.GetElementById(id);
        foreach (Forms.HtmlElement item in ddRouteSelected.Children)
        {
            if (item.InnerText != null && item.InnerText.ToLower().Equals(value.ToLower()))
            {
                item.SetAttribute("selected", "Selected");
                item.SetAttribute("value", value);
                ddRouteSelected.InvokeMember("onchange");
                break;
            }
        }
    }
like image 1
Dj Singh Avatar answered Nov 02 '22 22:11

Dj Singh