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.
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.
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");
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With