.NET Framework Class Library
HtmlDocument.GetElementById Method

Note: This method is new in the .NET Framework version 2.0.

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

Visual Basic (Declaration)
Public Function GetElementById ( _
    id As String _
) As HtmlElement
Visual Basic (Usage)
Dim instance As HtmlDocument
Dim id As String
Dim returnValue As HtmlElement

returnValue = instance.GetElementById(id)
C#
public HtmlElement GetElementById (
    string id
)
C++
public:
HtmlElement^ GetElementById (
    String^ id
)
J#
public HtmlElement GetElementById (
    String id
)
JScript
public function GetElementById (
    id : String
) : HtmlElement

Parameters

id

The ID attribute of the element to retrieve.

Return Value

Returns the first object with the same ID attribute as the specified value.
Remarks

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

Example

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.

Visual Basic
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 Not (TableElem Is 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
C#
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);
}
Platforms

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

Version Information

.NET Framework

Supported in: 2.0
See Also

Tags :


Page view tracker