RaiseEvent(“onchange”)

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

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.

回答1:

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.



回答2:

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


回答3:

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"); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!