次の方法で共有


カスタム コントロール ビルダのサンプル

カスタム コントロール ビルダ (NoWhiteSpaceControlBuilder) の定義とカスタム コントロール (NoWhiteSpaceControl) との関連付けのサンプルを次に示します。比較のために、カスタム ビルダを備えていない別のコントロールも含まれています。このサンプルをビルドする方法については、「サーバー コントロールのサンプル」の手順を参照してください。

namespace CustomControls 
{  
   using System;
   using System.Web.UI;
   
   public class NoWhiteSpaceControlBuilder : ControlBuilder 
   {  
      public override bool AllowWhitespaceLiterals() 
      {
         return false;
      }
   }
   
   [ ControlBuilderAttribute(typeof(NoWhiteSpaceControlBuilder))]
   public class NoWhiteSpaceControl : Control 
   {}
   
   public class WhiteSpaceControl : Control 
   {}
   
}
[Visual Basic]
Option Explicit
Option Strict

Imports System
Imports System.Web.UI

Namespace CustomControls
   Public Class NoWhiteSpaceControlBuilder
      Inherits ControlBuilder
      
      Public Overrides Function AllowWhitespaceLiterals() As Boolean
         Return False
      End Function
   End Class
   
   <ControlBuilderAttribute(GetType(NoWhiteSpaceControlBuilder))> _
   Public Class NoWhiteSpaceControl
      Inherits Control
   End Class
   
   Public Class WhiteSpaceControl
      Inherits Control
   End Class
End Namespace

カスタム コントロール ビルダのサンプルのテスト ページ

カスタム コントロール ビルダを備えた NoWhiteSpaceControl カスタム コントロールを次の .aspx ページで使用します。比較のために、このページには、既定のコントロール ビルダを備えたコントロール (WhiteSpaceControl) も含まれています。

<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>

<html>
<body>         
<form  runat=server>
<pre>             
<Custom:NoWhiteSpaceControl Id = "NoSpace" runat=server>
This control does not allow white spaces.
<asp:Label runat = server id = "nospacel1" Text = "First label inside no white space control." />

<asp:Label runat = server id = "nospacel2" Text = "Second label after white space ." />
</Custom:NoWhiteSpaceControl>
 
<br><br>             
<Custom:WhiteSpaceControl Id = "Space" runat=server>
This control allows white spaces.
<asp:Label runat = server id = "spacel1" Text = "First label inside white space control." />

 
<asp:Label runat = server id = "spacel2" Text = "Second label after white space." />
</Custom:WhiteSpaceControl>
</pre>            
</form>              
</body>           
</html>

参照

コントロール ビルダの概要 | ControlBuilderAttribute | コントロール解析、ParseChildrenAttribute、およびコントロール ビルダ