HtmlDocument.GetElementById(String) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Recupera un único HtmlElement mediante el atributo del ID elemento como clave de búsqueda.
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
Parámetros
- id
- String
Atributo ID del elemento que se va a recuperar.
Devoluciones
Devuelve el primer objeto con el mismo ID atributo que el valor especificado o null si no se encuentra .id
Ejemplos
En el ejemplo de código siguiente se recupera un elemento con nombre TABLE de un documento, se cuenta el número de filas y se muestra el resultado en la página web. El ejemplo de código requiere que tenga un WebBrowser control en el proyecto denominado WebBrowser1y que haya cargado una página web con un TABLE cuyo ID atributo sea 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
Comentarios
Si hay varios elementos en el documento con el mismo valor de identificador, GetElementById devolverá el primero que encuentre.