.NET Framework Class Library
HtmlDocument..::.GetElementById Method

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
)
Visual C++
public:
HtmlElement^ GetElementById(
    String^ id
)
JScript
public function GetElementById(
    id : String
) : HtmlElement

Parameters

id
Type: System..::.String
The ID attribute of the element to retrieve.

Return Value

Type: System.Windows.Forms..::.HtmlElement
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.

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.

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 (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
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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Page view tracker