System.Web.UI.WebControls


.NET Framework 类库
WebControl 类

用作定义 System.Web.UI.WebControls 命名空间中的所有控件的公共方法、属性和事件的基类。

命名空间:System.Web.UI.WebControls
程序集:System.Web(在 system.web.dll 中)

语法

Visual Basic(声明)
<ThemeableAttribute(True)> _
Public Class WebControl
    Inherits Control
    Implements IAttributeAccessor
Visual Basic(用法)
Dim instance As WebControl
C#
[ThemeableAttribute(true)] 
public class WebControl : Control, IAttributeAccessor
C++
[ThemeableAttribute(true)] 
public ref class WebControl : public Control, IAttributeAccessor
J#
/** @attribute ThemeableAttribute(true) */ 
public class WebControl extends Control implements IAttributeAccessor
JScript
ThemeableAttribute(true) 
public class WebControl extends Control implements IAttributeAccessor
备注

WebControl 类提供所有 Web 服务器控件的公共属性、方法和事件。通过设置在此类中定义的属性,可以控制 Web 服务器控件的外观和行为。例如,通过使用 BackColorForeColor 属性,可以分别控制控件的背景色和字体颜色。在可以显示边框的控件上,可以通过设置 BorderWidthBorderStyleBorderColor 属性,控制边框宽度、边框样式和边框颜色。Web 服务器控件的大小可以通过 HeightWidth 属性来指定。

控件的行为可以通过设置某些属性来指定。通过设置 Enabled 属性,可以启用和禁用控件。通过设置 TabIndex 属性,可以控制控件在 Tab 键顺序中的位置。通过设置 ToolTip 属性,可以为控件指定工具提示。

Note注意

并非所有的控件都支持此类中定义的每个属性。有关某一属性是否受支持的特定信息,请参见该特定控件的文档。

Note注意

根据浏览器的不同,此类中的某些属性以不同的方式呈现。有些属性根本不呈现,而有他属性呈现,但没有任何效果。HttpBrowserCapabilities 对象的 TagWriter 属性确定 Web 服务器控件的呈现方式。对于支持 HTML 4.0 的浏览器,TagWriter 属性 (Property) 将包含一个常规 HttpBrowserCapabilities 对象,并且大多数属性 (Property) 都将用 HTML 4.0 样式属性 (Attribute) 来呈现。不知道是否支持 HTML 4.0 的浏览器将使用 Html32TextWriter 对象。这会将样式属性自动映射到任何相关的 HTML 3.2 标记属性。某些情况下(比如对于 ForeColor 属性),样式属性将转换为附加标记,如 <font> 标记。某些情况下,不执行任何映射。有关属性如何在不同的浏览器中呈现的特定信息,请参见该特定属性的文档。

有关 WebControl 的实例的初始属性值列表,请参见 WebControl 构造函数。

示例

Visual Basic
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);
      }
   }
}
J#
package ControlTest; 
import System.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;

// Renders the following HTML: 
// <span onclick="alert('Hello');" style="color:Red;">Custom Contents</span>
public class MyWebControl extends WebControl
{
    public MyWebControl()
    {
         super(HtmlTextWriterTag.Span);
    } //MyWebControl

    protected void AddAttributesToRender(HtmlTextWriter writer)
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "alert('Hello');");
        writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "Red");
        super.AddAttributesToRender(writer);
    } //AddAttributesToRender

    /** @attribute System.Security.Permissions.PermissionSet(System.Security.
        Permissions.SecurityAction.Demand, Name = "FullTrust")
     */
    protected void RenderContents(HtmlTextWriter writer)
    {
        writer.Write("Custom Contents");
        super.RenderContents(writer);
    } //RenderContents
} //MyWebControl
.NET Framework 安全性

继承层次结构

System.Object
   System.Web.UI.Control
    System.Web.UI.WebControls.WebControl
       派生类
线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0
请参见

标记 :


Page view tracker