HtmlDocument.GetElementById Method (String)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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)

public HtmlElement GetElementById(
	string 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, or null if the id cannot be found.

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

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"))
            throw(new ArgumentException("No TABLE with an ID of " + tableID + " exists."));


.NET Framework
Available since 2.0
Return to top