HtmlDocument.GetElementById(String) Metodo

Definizione

Recupera un singolo HtmlElement oggetto usando l'attributo dell'elemento ID come chiave di ricerca.

public:
 System::Windows::Forms::HtmlElement ^ GetElementById(System::String ^ id);
public System.Windows.Forms.HtmlElement GetElementById (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

Parametri

id
String

Attributo ID dell'elemento da recuperare.

Restituisce

Restituisce il primo oggetto con lo stesso ID attributo del valore specificato oppure null se non è possibile trovare .id

Esempio

Nell'esempio di codice seguente viene recuperato un oggetto denominato TABLE da un documento, viene conteggiato il numero di righe e viene visualizzato il risultato nella pagina Web. L'esempio di codice richiede che nel progetto sia presente un controllo denominato WebBrowser1e che sia stata caricata una pagina Web con un il TABLE cui ID attributo è Table1.WebBrowser

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

Commenti

Se nel documento sono presenti più elementi con lo stesso valore ID, GetElementById restituirà il primo elemento trovato.

Si applica a

Vedi anche