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

前端 未结 2 2002
终归单人心
终归单人心 2020-12-18 09:49

So I have a listbox I want to change, that looks like this:

\"enter

How do I c

相关标签:
2条回答
  • 2020-12-18 10:19

    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.

    0 讨论(0)
  • 2020-12-18 10:38

    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");
    
    0 讨论(0)
提交回复
热议问题