次の方法で共有


HtmlTextWriter.AddStyleAttribute メソッド

HTML スタイル属性を HtmlTextWriter 出力ストリームに追加します。

オーバーロードの一覧

key パラメータで指定した HtmlTextWriterStyle 値に関連付けられた HTML スタイル属性とその属性の値を HtmlTextWriter 出力ストリームに追加します。

[Visual Basic] Overloads Public Overridable Sub AddStyleAttribute(HtmlTextWriterStyle, String)

[C#] public virtual void AddStyleAttribute(HtmlTextWriterStyle, string);

[C++] public: virtual void AddStyleAttribute(HtmlTextWriterStyle, String*);

[JScript] public function AddStyleAttribute(HtmlTextWriterStyle, String);

指定した HTML スタイル属性とその値を HtmlTextWriter 出力ストリームに追加します。

[Visual Basic] Overloads Public Overridable Sub AddStyleAttribute(String, String)

[C#] public virtual void AddStyleAttribute(string, string);

[C++] public: virtual void AddStyleAttribute(String*, String*);

[JScript] public function AddStyleAttribute(String, String);

指定した HTML スタイル属性をその値と共に HtmlTextWriter 出力ストリームに追加します。

[Visual Basic] Overloads Protected Overridable Sub AddStyleAttribute(String, String, HtmlTextWriterStyle)

[C#] protected virtual void AddStyleAttribute(string, string, HtmlTextWriterStyle);

[C++] protected: virtual void AddStyleAttribute(String*, String*, HtmlTextWriterStyle);

[JScript] protected function AddStyleAttribute(String, String, HtmlTextWriterStyle);

使用例

[Visual Basic, C#, C++] メモ   ここでは、AddStyleAttribute のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
' Create a namespace named HtmlTextWriter3 that contains a custom class 
' derived from HtmlTextWriter. A page must override the 
' CreateHtmlTextWriter method and assign a custom class,
' named htwThree, to render page content. When called,
' htwThree inserts <Font> HTML tags around any 
' <Label> HTML element that it renders.
Imports System
Imports System.IO
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Examples.AspNet
' A custom class that overrides the CreateHtmlTextWriter method.
' This page uses the StyledLabelHtmlWriter to render its content.  
Public Class MyPage
   Inherits Page
   
   Protected Overrides Function CreateHtmlTextWriter(writer As TextWriter) As HtmlTextWriter
      Return New StyledLabelHtmlWriter(writer)
   End Function 'CreateHtmlTextWriter
End Class 'MyPage

' The custom HtmlTextWriter class that overrides two versions
' of the RenderBeginTag method and one version of the
' RenderEndTag method. 

Public Class StyledLabelHtmlWriter
   Inherits HtmlTextWriter
   Private writer As TextWriter
   
   
   Public Sub New(writer As TextWriter)
      MyBase.New(writer)
      Me.writer = writer
   End Sub 'New
   
   
   Public Sub New(writer As TextWriter, tabString As String)
      MyBase.New(writer, tabString)
      
      Me.writer = writer
   End Sub 'New
   
   
   Overloads Public Overrides Sub RenderBeginTag(tagKey As HtmlTextWriterTag)

      Dim endTag As String      
      
      ' If the HTML element being rendered is a <Label>,
      ' render the opening tag of a Font element before it.
      If tagKey = HtmlTextWriterTag.Label Then
         ' Check whether a Color style attribute is 
         ' included on the Label. If not, use the
         ' AddStyleAttribute and GetStyleName methods to add one
         ' and set its value to red.
         If Not IsStyleAttributeDefined(HtmlTextWriterStyle.Color) Then
            AddStyleAttribute(GetStyleName(HtmlTextWriterStyle.Color), "red")
         End If
         writer.Write(TagLeftChar)
         ' Use the GetTagName method to ensure that the 
         ' <Font> element is written properly when the
         ' RenderBeginTag method is called.
         writer.Write(GetTagName(HtmlTextWriterTag.Font))
         writer.Write(SpaceChar)
         ' Use the GetAttributeName method to ensure that the 
         ' Size attribute is written properly.
         writer.Write(GetAttributeName(HtmlTextWriterAttribute.Size))
         writer.Write(EqualsChar)
         writer.Write(DoubleQuoteChar)
         writer.Write("30pt")
         writer.Write(DoubleQuoteChar)
         writer.Write(TagRightChar)
         
         ' The endTag variable must be declared as a string 
         ' in the class variable. Use the TagLeftChar,
         ' SlashChar, and TagRightChar fields and
         ' the GetTagName method to assign a value to
         ' the endTag variable.
         endTag = TagLeftChar & SlashChar & GetTagName(HtmlTextWriterTag.Font) & TagRightChar
                  
         ' Use the PushEndTag method to write the custom
         ' endTag string value as its parameter.
         PushEndTag(endTag)
      End If 
      ' Call the base class's RenderBeginTag method.
      MyBase.RenderBeginTag(tagKey)
   End Sub 'RenderBeginTag
   
   Overloads Public Overrides Sub RenderBeginTag(tagName As String)
      ' Call the overloaded RenderBeginTag(HtmlTextWriterTag) method.
      RenderBeginTag(GetTagKey(tagName))
   End Sub 'RenderBeginTag
   
   Public Overrides Sub RenderEndTag()
      ' Call the RenderEndTag method of the base class.
      MyBase.RenderEndTag()
      
      ' If the element being rendered is a Label, 
      ' call the PopEndTag method to write its closing tag.
      If TagKey = HtmlTextWriterTag.Label Then
         writer.Write(PopEndTag())
      End If
   End Sub 'RenderEndTag
End Class 'htwThree 


Public Class FirstControl
   Inherits WebControl
   
   Private myMessage As [String] = "Hello"   
   
   Public Overridable Property Message() As [String]
      Get
         Return myMessage
      End Get
      Set
         myMessage = value
      End Set
   End Property
   
   
   Protected Overrides Sub Render(writer As HtmlTextWriter)
      ' Write the contents of the control.
      writer.AddStyleAttribute("color", "blue")
      writer.RenderBeginTag("label")
      writer.Write(Message)
      writer.RenderEndTag()
      writer.AddAttribute("size", "30pt")
      writer.RenderBeginTag(HtmlTextWriterTag.Font)
      writer.Write(("<br>" + "The time on the server: " & System.DateTime.Now.ToLongTimeString()))      
      writer.RenderEndTag()
   End Sub 
End Class 
End Namespace 

[C#] 
/* Create a namespace, named HtmlTextWriter3, that contains a custom class derived from the HtmlTextWriter class. A page must override the
CreateHtmlTextWriter method and assign a custom class, named htwThree,
to render page content. When called, htwThree inserts <Font> HTML 
tags around any HTML <Label> elements that it renders.
*/
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Examples.AspNet 
{
   // A custom class that overrides the CreateHtmlTextWriter method.
   // This page uses the StyledLabelHtmlWriter class to render its content.  
   public class MyPage : Page 
   {
      protected override HtmlTextWriter CreateHtmlTextWriter(TextWriter writer)
      {
         return new StyledLabelHtmlWriter(writer);
      }

   }

   // A custom HtmlTextWriter class that overrides two versions
   // of the RenderBeginTag method and one version of the
   // RenderEndTag method. 
   public class StyledLabelHtmlWriter : HtmlTextWriter
   {
      private TextWriter writer;

      public StyledLabelHtmlWriter(TextWriter writer): base(writer)
      {
        this.writer = writer; 
      }

      public StyledLabelHtmlWriter(TextWriter writer, string tabString): base(writer, tabString)
      {
         this.writer = writer;
      }

      public override void RenderBeginTag(HtmlTextWriterTag tagKey)
      {
         string endTag;
         

         // If the HTML element being rendered is a <Label>,
         // render the opening tag of a <Font> element before it.
         if(tagKey == HtmlTextWriterTag.Label)
         {
           // Check whether a Color style attribute is 
           // included on the Label. If not, use the
           // AddStyleAttribute and GetStyleName methods to add one
           // and set its value to red.
           if(!IsStyleAttributeDefined(HtmlTextWriterStyle.Color))
           {
             AddStyleAttribute(GetStyleName(HtmlTextWriterStyle.Color), "red");
           }
            writer.Write(TagLeftChar);
            // Use the GetTagName method to ensure that the 
            // <Font> element is written properly when the
            // RenderBeginTag method is called.
            writer.Write(GetTagName(HtmlTextWriterTag.Font));
            writer.Write(SpaceChar);
            // Use the GetAttributeName method to ensure that the 
            // Size attribute is written properly.
            writer.Write(GetAttributeName(HtmlTextWriterAttribute.Size));
            writer.Write(EqualsChar);
            writer.Write(DoubleQuoteChar);
            writer.Write("30pt");
            writer.Write(DoubleQuoteChar);
            writer.Write(TagRightChar);

            // The endTag variable must be declared as a string
            // in the class variable. Use the TagLeftChar,
            // SlashChar, and TagRightChar fields and the 
            // GetTagName method to assign a value to the
            // endTag variable.
            endTag = TagLeftChar + SlashChar + GetTagName(HtmlTextWriterTag.Font) + TagRightChar;

            // Use the PushEndTag method to write the custom
            // endTag string value as its parameter.
            PushEndTag(endTag);
          }

          // Call the base class's RenderBeginTag method.
          base.RenderBeginTag(tagKey);
      }

      public override void RenderBeginTag(string tagName)
      {
         // Call the overloaded RenderBeginTag(HtmlTextWriterTag)
         // method.
         RenderBeginTag(GetTagKey(tagName));
      }

      public override void RenderEndTag()
      {
         // Call the RenderEndTag method of the base class.
         base.RenderEndTag();

         // If the element being rendered is a <Label>, 
         // call the PopEndTag method to write its closing tag.
         if(TagKey == HtmlTextWriterTag.Label)
         {
             writer.Write(PopEndTag());
         }                
      }
   }   

   public class FirstControl : WebControl
   {
      
      private String myMessage = "Hello";


      public virtual String Message 
      {
         get
         {
            return myMessage;
         }
         set 
         {
            myMessage = value;
         }
      }

      protected override void Render(HtmlTextWriter writer)
       {
          // Write the contents of the control.
          writer.AddStyleAttribute("color", "blue");
          writer.RenderBeginTag("label");
          writer.Write(Message);
          writer.RenderEndTag();
          writer.AddAttribute("size", "30pt");
          writer.RenderBeginTag(HtmlTextWriterTag.Font);
          writer.Write("<br>" + "The time on the server: " + System.DateTime.Now.ToLongTimeString());
          writer.RenderEndTag();
       }
   }
}

[C++] 
/* Create a namespace named HtmlTextWriter3 that contains a custom class derived from
HtmlTextWriter. A page must override the CreateHtmlTextWriter method and
assign a custom class, named htwThree, to render page content. When
called, htwThree inserts <Font> HTML tags around any <Label> HTML 
elements that it renders.
*/
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>

using namespace System;
using namespace System::IO;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;

// Create a custom HtmlTextWriter class that overrides two versions
// of the RenderBeginTag method and one version of the
// RenderEndTag method.
   public __gc class StyledLabelHtmlWriter : public HtmlTextWriter {
   private:
      TextWriter*  writer;

   public:
      StyledLabelHtmlWriter(TextWriter* writer): HtmlTextWriter(writer) {
         this->writer = writer;
      }

   public:
     StyledLabelHtmlWriter(TextWriter* writer, String* tabString): HtmlTextWriter(writer, tabString) {
         this->writer = writer;
      }

   public:
      void RenderBeginTag(HtmlTextWriterTag tagKey) {
         String* endTag;


         // If the HTML element being rendered is a <Label>,
         // render the opening tag of a <Font> element before it.
         if (tagKey == HtmlTextWriterTag::Label) {
            // Check whether a Color style attribute is
            // included on the Label. If not, use the
            // AddStyleAttribute and GetStyleName methods to add one
            // and set its value to red.
            if (!IsStyleAttributeDefined(HtmlTextWriterStyle::Color)) {
               AddStyleAttribute(GetStyleName(HtmlTextWriterStyle::Color), S"red");
            }
            writer->Write(TagLeftChar);
            // Use the GetTagName method to ensure that the
            // <Font> element is written properly when the
            // RenderBeginTag method is called.
            writer->Write(GetTagName(HtmlTextWriterTag::Font));
            writer->Write(SpaceChar);
            // Use the GetAttributeName method to ensure that the
            // Size attribute is written properly.
            writer->Write(GetAttributeName(HtmlTextWriterAttribute::Size));
            writer->Write(EqualsChar);
            writer->Write(DoubleQuoteChar);
            writer->Write(S"30pt");
            writer->Write(DoubleQuoteChar);
            writer->Write(TagRightChar);

            // The endTag variable must be declared as a String* in the class
            // variable for this assignment to work. Use the
            // TagLeftChar, SlashChar, and TagRightChar fields and the
            // GetTagName method to assign a value to the endTag variable.
            endTag = String::Concat( __box(TagLeftChar), __box(SlashChar), GetTagName(HtmlTextWriterTag::Font), __box(TagRightChar));

            // Use the PushEndTag method to write the custom
            // endTag String* value as its parameter.
            PushEndTag(endTag);
         }

         // Call the base class's RenderBeginTag method.
         __super::RenderBeginTag(tagKey);
      }

   public:
      void RenderBeginTag(String* tagName) {
         // Call the overloaded RenderBeginTag(HtmlTextWriterTag) method.
         RenderBeginTag(GetTagKey(tagName));
      }

   public:
      void RenderEndTag() {
         // Call the RenderEndTag method of the base class.
         __super::RenderEndTag();

         // If the element being rendered is a <Label>,
         // call the PopEndTag method to write its closing tag.
         if (TagKey == HtmlTextWriterTag::Label) {
            writer->Write(PopEndTag());
         }
      }
   };

   public __gc class FirstControl : public WebControl {
      
   private:
      String* myMessage;

   public:
      FirstControl()
      {
         myMessage = S"Hello";
      }

      // Accessors for the Message property.
      __property virtual String* get_Message() {
         return myMessage;
      }
      __property virtual void set_Message(String* value) {
         myMessage = value;
      }

   protected:
      void Render(HtmlTextWriter* writer) {
         // Write the contents of the control.
         writer->AddStyleAttribute(S"color", S"blue");
         writer->RenderBeginTag(S"label");
         writer->Write(Message);
         writer->RenderEndTag();
         writer->AddAttribute(S"size", S"30pt");
         writer->RenderBeginTag(HtmlTextWriterTag::Font);
         writer->Write(S"<br> The time on the server: {0}",
            System::DateTime::Now.ToLongTimeString());
         writer->RenderEndTag();
      }
   };

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

HtmlTextWriter クラス | HtmlTextWriter メンバ | System.Web.UI 名前空間