How do I change a dropdown box in a webbrowser control?

邮差的信 提交于 2019-11-29 07:48:55

View the HTML of the website and identify the id and values of the dropdownlist, for example:

<select id="bdayMonthId" size="1" name="bdayMonth">
    <option value="">Month</>
    <option value="Jan">January</>
    <option value="Feb">February</>
    <option value="Mar">March</>
</select>

To pre-select the dropdownlist value in the WebBrowser control use this Winform code:

webBrowser1.Document.GetElementById("bdayMonthId").SetAttribute("value", "Feb");
Victor Stoddard

I'm a bit confused about your question. If you just want to alter an element in the HTML, Jeremy's answer is the best, and simplest, way to go. If you wanted to call the document's javascript, this should work:

Let's assume that the webbrowser's document contains the following HTML:

<html>
    <head>
    <title>Invoke Test</title>
    </head>
    <body>
        <div id="testdiv">Waiting...</div>
        <script>
            function changeDate(date) {
                var x=document.getElementById("testdiv");
                x.innerHTML = date;
            }
        </script>
    </body>
</html>

To invoke the webbrowser document's javascript method, you can use something like this:

private void button1_Click(object sender, EventArgs e)
{
    object o = webBrowser1.Document.InvokeScript("changeDate('june')");
}

No System.Web, ASP, ScriptManagers or Interop are needed. All the tools you need to control webbrowser and document objects and events come with the webbrowser control.

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