Bearbeiten

Freigeben über


HtmlDocument.GetElementById(String) Method

Definition

Retrieves a single HtmlElement using the element's ID attribute as a search key.

public:
 System::Windows::Forms::HtmlElement ^ GetElementById(System::String ^ id);
public System.Windows.Forms.HtmlElement GetElementById (string id);
public System.Windows.Forms.HtmlElement? GetElementById (string id);
member this.GetElementById : string -> System.Windows.Forms.HtmlElement
Public Function GetElementById (id As String) As HtmlElement

Parameters

id
String

The ID attribute of the element to retrieve.

Returns

Returns the first object with the same ID attribute as the specified value, or null if the id cannot be found.

Examples

The following code example retrieves a named TABLE from a document, counts up the number of rows, and displays the result in the Web page. The code example requires that you have a WebBrowser control in your project named WebBrowser1, and that you have loaded a Web page with a TABLE whose ID attribute is Table1.

private Int32 GetTableRowCount(string tableID)
{
    Int32 count = 0;

    if (webBrowser1.Document != null)
    {
        HtmlElement tableElem = webBrowser1.Document.GetElementById(tableID);
        if (tableElem != null)
        {
            foreach (HtmlElement rowElem in tableElem.GetElementsByTagName("TR"))
            {
                count++;
            }
        }
        else
        {
            throw(new ArgumentException("No TABLE with an ID of " + tableID + " exists."));
        }
    }

    return(count);
}
Private Function GetTableRowCount(ByVal TableID As String) As Integer
    Dim Count As Integer = 0

    If (WebBrowser1.Document IsNot Nothing) Then

        Dim TableElem As HtmlElement = WebBrowser1.Document.GetElementById(TableID)
        If (TableElem IsNot Nothing) Then
            For Each RowElem As HtmlElement In TableElem.GetElementsByTagName("TR")
                Count = Count + 1
            Next
        Else
            Throw (New ArgumentException("No TABLE with an ID of " & TableID & " exists."))
        End If
    End If
    GetTableRowCount = Count
End Function

Remarks

If there are multiple elements in the document with the same ID value, GetElementById will return the first one it finds.

Applies to

See also