简体   繁体   English

如何在C#webBrowser控件中获取SELECT HtmlElement的值

[英]How to get the value of a SELECT HtmlElement in C# webBrowser control

In a C# WebBrowser control, I have generated a SELECT HtmlElement with a number of OPTION elements using w.RenderBeginTag(HtmlTextWriterTag.Select). 在C#WebBrowser控件中,我使用w.RenderBeginTag(HtmlTextWriterTag.Select)生成了带有多个OPTION元素的SELECT HtmlElement。

I need to get the value of the select when the user changes it, and so added an event handler in the WebBrowser DocumentCompleted event. 我需要在用户更改选择时获取选择的值,因此在WebBrowser DocumentCompleted事件中添加了事件处理程序。

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    ....

    webBrowser1.Document.GetElementById("id_select_0").AttachEventHandler("onchange", new EventHandler(ddSelectedIndexChanged));
    ....
}

protected void ddSelectedIndexChanged(object sender, EventArgs e)
{
    ....
}

The event handler does get called, but the sender parameter is null and e is empty. 确实会调用事件处理程序,但sender参数为null,e为空。 Does anyone know how to overcome this problem? 有谁知道如何克服这个问题?

Andrew 安德鲁

It is not possible get the event parameters from the DHTML event because the event handler is the general EventHandler class. 由于事件处理程序是常规EventHandler类,因此无法从DHTML事件获取事件参数。 Alternative way is to get the parameters from IHTMLEventObj, this object contains the same event parameters as HtmlElementEventArgs: 另一种方法是从IHTMLEventObj获取参数,此对象包含与HtmlElementEventArgs相同的事件参数:

private void Element_OnChange(object sender, EventArgs e)
{
    IHTMLEventObj ev = (webBrowser1.Document.Window.DomWindow as IHTMLWindow2).@event;
}

You can use MSHTML.dll for your references. 您可以使用MSHTML.dll作为参考。 Like this: 像这样:

Firstly declarate your list as : 首先将您的清单声明为:

Private WithEvents list1 As MSHTML.HTMLSelectElement

then in form sub : 然后以形式sub:

Dim htmldoc As MSHTML.HTMLFormElement = WebBrowser1.Document.DomDocument.getElementById("f")
list= htmldoc.item("...")
AddHandler list.onchange, AddressOf ddSelectedIndexChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM