HtmlDocument.GetElementById(String) Metoda

Definicja

Pobiera jeden HtmlElement przy użyciu atrybutu ID elementu jako klucza wyszukiwania.

public:
 System::Windows::Forms::HtmlElement ^ GetElementById(System::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

Parametry

id
String

Atrybut ID elementu do pobrania.

Zwraca

HtmlElement

Zwraca pierwszy obiekt o tym samym atrybucie ID co określona wartość lub null jeśli id nie można go odnaleźć.

Przykłady

Poniższy przykład kodu pobiera nazwę TABLE z dokumentu, zlicza liczbę wierszy i wyświetla wynik na stronie sieci Web. Przykładowy kod wymaga WebBrowser kontrolki w projekcie o nazwie WebBrowser1i załadowaniu strony sieci Web z atrybutem , którego TABLE ID atrybutem jest 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

Uwagi

Jeśli w dokumencie znajduje się wiele elementów o tej samej wartości identyfikatora, GetElementById zostanie zwrócona pierwsza z nich.

Dotyczy

Zobacz też