次の方法で共有


WebControl クラス

System.Web.UI.WebControls 名前空間のすべてのコントロールに共通のメソッド、プロパティおよびイベントを定義する基本クラスとして機能します。

この型のすべてのメンバの一覧については、WebControl メンバ を参照してください。

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.WebControl
         派生クラス

Public Class WebControl
   Inherits Control
   Implements IAttributeAccessor
[C#]
public class WebControl : Control, IAttributeAccessor
[C++]
public __gc class WebControl : public Control, IAttributeAccessor
[JScript]
public class WebControl extends Control implements
   IAttributeAccessor

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

WebControl クラスには、すべての Web サーバー コントロールに共通するプロパティ、メソッド、およびイベントが用意されています。このクラスで定義されたプロパティを設定することによって、Web サーバー コントロールの外観と動作を制御できます。たとえば、コントロールの背景色とフォントの色を制御するには、 BackColor プロパティと ForeColor プロパティをそれぞれ設定します。境界線を表示できるコントロールで、境界線の幅、境界線スタイル、および境界線の色を制御するには、 BorderWidthBorderStyleBorderColor の各プロパティをそれぞれ設定します。Web サーバー コントロールのサイズを指定するには、 Height プロパティと Width プロパティを使用します。

コントロールの動作は、特定のプロパティを設定して指定できます。コントロールを有効または無効にするには、 Enabled プロパティを設定します。タブ オーダーのコントロールの位置を制御するには、 TabIndex プロパティを設定します。コントロールのツール ヒントを指定するには、 ToolTip プロパティを設定します。

メモ   このクラスで定義されるプロパティを一部しかサポートしないコントロールもあります。プロパティがサポートされるかどうかについては、特定のコントロールに関するドキュメントを参照してください。

メモ   このクラスのプロパティの中には、ブラウザによって表示方法が異なるプロパティがあります。まったく表示されないプロパティもあれば、表示には影響を与えないプロパティもあります。 System.Web.HttpBrowserCapabilities オブジェクトの HttpBrowserCapabilities.TagWriter プロパティによって、Web サーバー コントロールでの表示方法を決定します。HTML 4.0 をサポートするブラウザの場合、 HttpBrowserCapabilities.TagWriter プロパティは通常の System.Web.UI.HtmlTextWriter オブジェクトを示し、ほとんどのプロパティは HTML 4.0 スタイル属性を使用して表示されます。HTML 4.0 をサポートすることが認識されないブラウザは、 System.Web.UI.Html32TextWriter オブジェクトを使用します。これによって、スタイル属性は、関連の HTML 3.2 タグ属性に自動的に変換されます。 ForeColor プロパティを使用した場合などに、スタイル属性が、 <font> タグなどの追加のタグに変換される場合があります。また、マップが実行されない場合もあります。プロパティを各種ブラウザに表示する方法については、特定のプロパティに関するドキュメントを参照してください。

WebControl のインスタンスの初期プロパティ値の一覧については、 WebControl コンストラクタのトピックを参照してください。

使用例

 

Imports System
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 'New      
      
      <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 'AddAttributesToRender       

      <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 'RenderContents

   End Class 'MyWebControl

End Namespace 'ControlTest


[C#] 
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);
      }
   }
}


[C++] 
#using <mscorlib.dll>
#using <System.Web.dll>
#using <System.dll>
using namespace System;
using namespace System::Web::UI;
using namespace System::Web::UI::WebControls;

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

public __gc class MyWebControl : public WebControl 
{
public:
   MyWebControl() : WebControl(HtmlTextWriterTag::Span) { }

protected:

   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
   void AddAttributesToRender(HtmlTextWriter* writer) 
   {
      writer->AddAttribute(HtmlTextWriterAttribute::Onclick, S"alert('Hello');");
      writer->AddStyleAttribute(HtmlTextWriterStyle::Color, S"Red");
      __super::AddAttributesToRender(writer);
   }

protected:

   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
   void RenderContents(HtmlTextWriter* writer) 
   {
      writer->Write(S"Custom Contents");
      __super::RenderContents(writer);
   }
};

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

必要条件

名前空間: System.Web.UI.WebControls

プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Web (System.Web.dll 内)

参照

WebControl メンバ | System.Web.UI.WebControls 名前空間 | BackColor | ForeColor | BorderWidth | BorderStyle | BorderColor | Height | Width | Enabled | TabIndex | ToolTip