HtmlElement.SetAttribute Method (String, String)


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


Type: System.String

The name of the attribute to set.

Type: System.String

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.

private void InsertImageFooter()
	if (webBrowser1.Document != null)
		HtmlDocument doc = webBrowser1.Document;
		HtmlElement elem = doc.CreateElement("IMG");
		elem.SetAttribute("SRC", "");


.NET Framework
Available since 2.0
Return to top