AttributeCollection.AddAttributes(HtmlTextWriter) 方法

定义

将特性从 AttributeCollection 类添加到 HtmlTextWriter 对象中,该对象负责将这些特性以标记形式呈现。

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)

参数

writer
HtmlTextWriter

HtmlTextWriter 实例,它将这些特性写入 ASP.NET 服务器控件的开始标记中。

示例

以下示例演示如何将新属性添加到 Button 名为 myButton 的服务器控件和 TextBox 名为 myTextBox的服务器控件。 然后,它将这些属性添加到自定义 HtmlTextWriter 对象,该对象会将这些属性写入每个控件的输出流。

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)

注解

此方法将所有服务器控件的属性复制到对象,以便下一 HtmlTextWriter 次调用 RenderBeginTag 该方法可以呈现它们。

对于包含在其他控件中的控件 Calendar ,例如, CheckBoxList可以通过直接呈现包含的控件来提高性能。 直接呈现比通过容器控件呈现更快,因为容器控件要求将所有子控件属性复制到容器控件,并在复制它们后呈现它们。

适用于

另请参阅