HtmlDocument.GetElementById(String) Method

Definition

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

C#
public System.Windows.Forms.HtmlElement GetElementById(string id);
C#
public System.Windows.Forms.HtmlElement? GetElementById(string id);

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.

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);
}

Remarks

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

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also