Export (0) Print
Expand All

HtmlElement.SetAttribute Method

Note: This method is new in the .NET Framework version 2.0.

Sets the value of the named attribute on the element.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public void SetAttribute (
	string attributeName,
	string value
public void SetAttribute (
	String attributeName, 
	String value
public function SetAttribute (
	attributeName : String, 
	value : String



The name of the attribute to set.


The new value of this attribute.

An attribute in HTML is any valid name-value pair for that element. HtmlElement exposes only those attributes that are common to all elements, leaving out those that only apply to certain types of elements; SRC is a predefined attribute for the IMG tag, for example, but not for the DIV tag. Use GetAttribute and SetAttribute to manipulate attributes not exposed on the managed Document Object Model (DOM).

If attributeName is not a defined attribute on an element, SetAttribute will define it on the element as a new attribute.

GetAttribute and SetAttribute are case-insensitive.

To set the class attribute on an HtmlElement , you must refer to the attribute as className when specifying the first argument to SetAttribute

The following code example adds a new IMG element to the current document, using SetAttribute to set the SRC attribute for the image.

HtmlElement bodyElement = null;

private void SetBody()
    if (webBrowser1.Document != null)
        HtmlDocument doc = webBrowser1.Document;
        bodyElement = doc.Body;
        //bodyElement.Error += new HtmlElementEventHandler(HandleError);

private void HandleError(Object sender, HtmlElementEventArgs e)
    //System.Diagnostics.EventLog.WriteEntry("Custom Application", "Scripting error occurred: " & _

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

© 2014 Microsoft