AttributeCollection.AddAttributes Method (HtmlTextWriter)

 

Adds attributes from the AttributeCollection class to the HtmlTextWriter object that is responsible for rendering the attributes as markup.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public void AddAttributes(
	HtmlTextWriter writer
)

Parameters

writer
Type: System.Web.UI.HtmlTextWriter

An HtmlTextWriter instance that writes the attribute to the opening tag of an ASP.NET server control.

This method copies all the server control's attributes to an HtmlTextWriter object so that they can be rendered by the next call to the RenderBeginTag method.

For controls that are contained in other controls such as Calendar and CheckBoxList, you can potentially improve performance by rendering the contained controls directly. Rendering directly can be faster than rendering through the container control because container controls require that you copy all the child control attributes to the container control and render them after they are copied.

The following example shows how to add new attributes to a Button server control named myButton and a TextBox server control named myTextBox. It then adds those attributes to a custom HtmlTextWriter object that will write those attributes to an output stream for each of those controls.

myButton.Attributes.Clear();
myTextBox.Attributes.Clear();
myButton.Attributes["onClick"] = 
                "javascript:alert('Visiting msn.com');";

myTextBox.Attributes["name"] = "MyTextBox";

myTextBox.Attributes["onBlur"] = 
                 "javascript:alert('Leaving MyTextBox...');";

HttpResponse myHttpResponse = Response;
HtmlTextWriter myHtmlTextWriter  = 
                 new HtmlTextWriter(myHttpResponse.Output);

myButton.Attributes.AddAttributes(myHtmlTextWriter);
myTextBox.Attributes.AddAttributes(myHtmlTextWriter);

.NET Framework
Available since 1.1
Return to top
Show: