0

I know this may sound a little strange, but I would like to extract the values and text of an HTML select tag. For example let's say I had an HTML page with this select tag in it

<select name="selectName">
    <option value="1">Value 1 text</option>
    <option value="2">Value 2 text</option>
</select>

Is there any way I could loop through that specific select tag and extract each value and each displayed text with VB.NET

Maybe something like this:

For Each option In selectName
    MsgBox(option.value.ToString + " " + option.Text)
Next

Any help with this would be greatly appreciated.

slister
  • 769
  • 1
  • 13
  • 29
  • Are u running an ASP.NET application? Then by adding an ID and `runat="server"` attribute you can make it a server-side control and use VB.NET. Otherwise you're limited to client-side JavaScript – Yuriy Galanter Aug 19 '14 at 20:38
  • Does the application need to query the page in real time? Or are you aware of what the values are, ie they are an unchanging list? – Bill Gregg Aug 19 '14 at 20:39
  • @Yuriy I don't think he's limited to client side javascript. If he needs to call out to the page via WebRequest he could, and then parse the result. Not an ideal solution, but a solution. – Bill Gregg Aug 19 '14 at 20:40
  • The application will query from an active web page, so yes those values could potentially change. – slister Aug 19 '14 at 20:40
  • @slister or are u hosting the web page in a WebBrowser control of a WinForm app? – Yuriy Galanter Aug 19 '14 at 20:42
  • I will be using a WebBrowswer control on a WinForm app yes. – slister Aug 19 '14 at 20:44

2 Answers2

1

You can achieve this by adding a handle to the DocumentCompleted event and use LINQ to query the HtmlDocument.

Example:

enter image description here

Option Strict On

Public Class Form1

    Public Sub New()
        Me.InitializeComponent()
        Me.Button1 = New Button() With {.TabIndex = 0, .Dock = DockStyle.Top, .Text = "Load web page", .Height = 30}
        Me.WebBrowser1 = New WebBrowser() With {.TabIndex = 1, .Dock = DockStyle.Fill}
        Me.ComboBox1 = New ComboBox() With {.TabIndex = 2, .Dock = DockStyle.Bottom, .DropDownStyle = ComboBoxStyle.DropDownList}
        Me.Controls.AddRange({Me.ComboBox1, Me.WebBrowser1, Me.Button1})
    End Sub

    Private Sub HandleButtonClick(sender As Object, e As EventArgs) Handles Button1.Click
        Me.WebBrowser1.DocumentText = <!--
            <!DOCTYPE html>
            <html>
                <body>
                    <select name="selectName">
                      <option value="1">Value 1 text</option>
                      <option value="2">Value 2 text</option>
                      <option value="3">Value 3 text</option>
                      <option value="4">Value 4 text</option>
                    </select>
                </body>
            </html>
        -->.Value
    End Sub

    Private Sub HandleWebBrowserDocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If (Not Me.WebBrowser1.Document Is Nothing) Then

            Dim selectTag As HtmlElement = (
                From
                    element As HtmlElement
                In
                    Me.WebBrowser1.Document.GetElementsByTagName("select").Cast(Of HtmlElement)()
                Where
                    element.Name = "selectName"
                Select
                    element
            ).FirstOrDefault()

            If (Not selectTag Is Nothing) Then

                Dim options As List(Of KeyValuePair(Of Integer, String)) = (
                    From
                        element As HtmlElement
                    In
                        selectTag.GetElementsByTagName("option").Cast(Of HtmlElement)()
                    Select
                        New KeyValuePair(Of Integer, String)(Integer.Parse(element.GetAttribute("value")), element.InnerText)
                ).ToList()

                Me.ComboBox1.DataSource = options
                Me.ComboBox1.ValueMember = "Key"
                Me.ComboBox1.DisplayMember = "Value"

            End If

        End If
    End Sub

    Private WithEvents Button1 As Button
    Private WithEvents WebBrowser1 As WebBrowser
    Private WithEvents ComboBox1 As ComboBox

End Class
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
0

You need to add id and runat to your html control to access it in code behind. hence your select tag will be as follows:

  <select name="selectName" id ="cbosample" runat ="server">
    <option value="1">Value 1 text</option>
    <option value="2">Value 2 text</option>
  </select>

Now the controls are accessible in code behind. code to display it in alert box as:

 For i As Integer = 0 To cbosample.Items.Count - 1 '  tems
     MsgBox(cbosample.Items(i).ToString)
 Next 
Community
  • 1
  • 1