WebControl.AddAttributesToRender(HtmlTextWriter) 方法

定义

将需要呈现的 HTML 特性和样式添加到指定的 HtmlTextWriterTag 中。 此方法主要由控件开发人员使用。

protected:
 virtual void AddAttributesToRender(System::Web::UI::HtmlTextWriter ^ writer);
protected virtual void AddAttributesToRender (System.Web.UI.HtmlTextWriter writer);
abstract member AddAttributesToRender : System.Web.UI.HtmlTextWriter -> unit
override this.AddAttributesToRender : System.Web.UI.HtmlTextWriter -> unit
Protected Overridable Sub AddAttributesToRender (writer As HtmlTextWriter)

参数

writer
HtmlTextWriter

表示在客户端上呈现 HTML 内容的输出流的 HtmlTextWriter

示例

以下示例演示如何创建派生自 类的 WebControl 类。 将重写 方法的基本 AddAttributesToRender 实现,以在输出流中添加适合派生类的属性 HtmlTextWriter

namespace ControlTest 
{
   using System;
   using System.Web.UI;
   using System.Web.UI.WebControls;

   // Renders the following HTML: 
   // <span onclick="alert('Hello');" style="color:Red;">Custom Contents</span>

   public class MyWebControl: WebControl {

      public MyWebControl() : base(HtmlTextWriterTag.Span) 
      { }

      [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
      protected override void AddAttributesToRender(HtmlTextWriter writer) 
      {

         writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');");
         writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");
         base.AddAttributesToRender(writer);
      }

      [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] 
      protected override void RenderContents(HtmlTextWriter writer) 
      {
         writer.Write("Custom Contents");
         base.RenderContents(writer);
      }
   }
}

Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace ControlTest
   
   ' Renders the following HTML: 
   ' <span onclick="alert('Hello');" style="color:Red;">Custom Contents</span>
   Public Class MyWebControl
      Inherits WebControl
      
      
      Public Sub New()
         MyBase.New(HtmlTextWriterTag.Span)
      End Sub
      
      <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
      Protected Overrides Sub AddAttributesToRender(writer As HtmlTextWriter)
         
         writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');")
         writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red")
         MyBase.AddAttributesToRender(writer)

      End Sub

      <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
      Protected Overrides Sub RenderContents(writer As HtmlTextWriter)
         writer.Write("Custom Contents")
         MyBase.RenderContents(writer)
      End Sub

   End Class

End Namespace 'ControlTest

注解

若要在客户端上呈现 Web 服务器控件的属性和样式,通常调用 AddAttributeHtmlTextWriter.AddStyleAttribute 方法将每个属性和样式分别插入到输出流中 HtmlTextWriter 。 为了简化该过程,此方法封装了与 Web 服务器控件关联的每个属性和样式对 和 HtmlTextWriter.AddStyleAttribute 方法的所有调用HtmlTextWriter.AddAttribute。 所有属性和样式都通过单个方法调用插入到 HtmlTextWriter 输出流中。 派生类中的控件开发人员通常会重写此方法,以便将适当的属性和样式插入到类的 HtmlTextWriter 输出流中。

注意

方法 AddAttributesToRender 不能用于插入客户端脚本。 若要使用客户端脚本,请参阅 ClientScriptManager 类。

适用于

另请参阅