Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
HtmlElement Class
HtmlElement Methods
 SetAttribute Method
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2010/.NET Framework 4

Other versions are also available for the following:
.NET Framework Class Library
HtmlElement..::.SetAttribute Method

Sets the value of the named attribute on the element.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic
Public Sub SetAttribute ( _
    attributeName As String, _
    value As String _
)
C#
public void SetAttribute(
    string attributeName,
    string value
)
Visual C++
public:
void SetAttribute(
    String^ attributeName, 
    String^ value
)
F#
member SetAttribute : 
        attributeName:string * 
        value:string -> unit 

Parameters

attributeName
Type: System..::.String
The name of the attribute to set.
value
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.

Visual Basic
Private Sub InsertImageFooter()
    If (WebBrowser1.Document IsNot Nothing) Then
        With WebBrowser1.Document
            Dim Elem As HtmlElement = .CreateElement("IMG")
            Elem.SetAttribute("SRC", "http://www.adatum.com/images/footer-banner.jpg")

            .Body.AppendChild(Elem)
        End With
    End If
End Sub
C#
        private void InsertImageFooter()
        {
            if (webBrowser1.Document != null)
            {
                HtmlDocument doc = webBrowser1.Document;
                HtmlElement elem = doc.CreateElement("IMG");
                elem.SetAttribute("SRC", "http://www.adatum.com/images/footer-banner.jpg");

                doc.Body.AppendChild(elem);
            }
        }

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker