ページまたは ASP.NET サーバー コントロールのすべてのプロパティで HtmlTextWriter.OnAttributeRender と HtmlTextWriter.OnStyleAttributeRender を呼び出して、すべての HTML 属性とスタイル属性をフィルタ処理します。
Protected Overridable Sub FilterAttributes()
[C#]
protected virtual void FilterAttributes();
[C++]
protected: virtual void FilterAttributes();
[JScript]
protected function FilterAttributes();
解説
このメソッドは、 HtmlTextWriter クラスから継承するときにだけオーバーライドします。このメソッドは、HTML 要素に属性を表示する前に呼び出されます。次に、表示される各属性とスタイルに対して、 OnAttributeRender と OnStyleAttributeRender を呼び出します。
使用例
[Visual Basic, C#, C++] HtmlTextWriter クラスを拡張するカスタム クラスを作成する例を次に示します。このクラスでは FilterAttributes メソッドをオーバーライドし、出力する要素が HTML の <label> 要素または <a> 要素 (アンカ) であるかどうかをチェックします。出力する要素が <label> の場合は、この要素に HTML の style 属性があるかどうかをチェックし、ない場合は、 style 属性を作成して color: blue を設定しています。出力する要素が <a> の場合は、 href 属性が含まれているかどうかをチェックし、含まれていない場合は、URL http://www.cohowinery.com に href を追加します。
' Override the FilterAttributes method to filter for
' an HTML label and an anchor elements' attributes.
Protected Overrides Sub FilterAttributes()
' If the Label tag is being rendered and a style
' attribute is not defined, add a style attribute and set
' its value to blue.
If TagKey = HtmlTextWriterTag.Label Then
If Not IsAttributeDefined(HtmlTextWriterAttribute.Style) Then
AddAttribute("style", EncodeAttributeValue("color:blue", True))
Write(NewLine)
Indent = 3
OutputTabs()
End If
End If
' If an Anchor element is being rendered and an href
' attribute has not been defined, call the AddAttribute
' method to add an href
' attribute and set it to http://www.cohowinery.com.
' Use the EncodeUrl method to convert any spaces to %20.
If TagKey = HtmlTextWriterTag.A Then
If Not IsAttributeDefined(HtmlTextWriterAttribute.Href) Then
AddAttribute("href", EncodeUrl("http://www.cohowinery.com"))
End If
End If
' Call the base class's FilterAttributes method
' to ensure that calling this custom HtmlTextWriter
' includes functionality for all other HTML elements.
MyBase.FilterAttributes()
End Sub 'FilterAttributes
[C#]
// Override the FilterAttributes method to filter for
// an HTML label and an anchor elements' attributes.
protected override void FilterAttributes()
{
// If the Label tag is being rendered and a style
// attribute is not defined, add a style attribute and set
// its value to blue.
if(TagKey == HtmlTextWriterTag.Label)
{
if(!IsAttributeDefined(HtmlTextWriterAttribute.Style))
{
AddAttribute("style", EncodeAttributeValue("color:blue", true));
Write(NewLine);
Indent = 3;
OutputTabs();
}
}
// If an Anchor element is being rendered and an href
// attribute has not been defined, call the AddAttribute
// method to add an href
// attribute and set it to http://www.cohowinery.com.
// Use the EncodeUrl method to convert any spaces to %20.
if(TagKey == HtmlTextWriterTag.A)
{
if(!IsAttributeDefined(HtmlTextWriterAttribute.Href))
{
AddAttribute("href", EncodeUrl("http://www.cohowinery.com"));
}
}
// Call the base class's FilterAttributes method
// to ensure that calling this custom HtmlTextWriter
// includes functionality for all other HTML elements.
base.FilterAttributes();
}
[C++]
// Override the FilterAttributes method to filter for
// an HTML label and an anchor elements' attributes.
protected:
void FilterAttributes() {
// If the Label tag is being rendered and a style
// attribute is not defined, add a style attribute and set
// its value to blue.
if (TagKey == HtmlTextWriterTag::Label) {
if (!IsAttributeDefined(HtmlTextWriterAttribute::Style)) {
AddAttribute(S"style", EncodeAttributeValue(S"color:blue", true));
Write(NewLine);
Indent = 3;
OutputTabs();
}
}
// If an Anchor element is being rendered and an href
// attribute has not been defined, call the AddAttribute
// method to add an href attribute and set it to http://www.cohowinery.com.
// Use the EncodeUrl method to convert any spaces to %20.
if (TagKey == HtmlTextWriterTag::A) {
if (!IsAttributeDefined(HtmlTextWriterAttribute::Href)) {
AddAttribute(S"href", EncodeUrl(S"http://www.cohowinery.com"));
}
}
// Call the base class's FilterAttributes method
// to ensure that calling this custom HtmlTextWriter
// includes functionality for all other HTML elements.
__super::FilterAttributes();
}
[Visual Basic, C#, C++]
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン
をクリックします。
必要条件
プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ
参照
HtmlTextWriter クラス | HtmlTextWriter メンバ | System.Web.UI 名前空間