Edit

Bagikan melalui


AttributeCollection.AddAttributes(HtmlTextWriter) Method

Definition

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

public:
 void AddAttributes(System::Web::UI::HtmlTextWriter ^ writer);
public void AddAttributes (System.Web.UI.HtmlTextWriter writer);
member this.AddAttributes : System.Web.UI.HtmlTextWriter -> unit
Public Sub AddAttributes (writer As HtmlTextWriter)

Parameters

writer
HtmlTextWriter

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

Examples

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);
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...');"

Dim myHttpResponse As HttpResponse = Response
Dim myHtmlTextWriter As New HtmlTextWriter(myHttpResponse.Output)

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

Remarks

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.

Applies to

See also