CheckBox 类

显示允许用户选择 truefalse 条件的复选框。

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

语法

声明
<ControlValuePropertyAttribute("Checked")> _
Public Class CheckBox
    Inherits WebControl
    Implements IPostBackDataHandler, ICheckBoxControl
用法
Dim instance As CheckBox
[ControlValuePropertyAttribute("Checked")] 
public class CheckBox : WebControl, IPostBackDataHandler, ICheckBoxControl
[ControlValuePropertyAttribute(L"Checked")] 
public ref class CheckBox : public WebControl, IPostBackDataHandler, ICheckBoxControl
/** @attribute ControlValuePropertyAttribute("Checked") */ 
public class CheckBox extends WebControl implements IPostBackDataHandler, ICheckBoxControl
ControlValuePropertyAttribute("Checked") 
public class CheckBox extends WebControl implements IPostBackDataHandler, ICheckBoxControl

备注

使用 CheckBox 控件可以允许用户选择 true 状态或 false 状态。

如果您计划使用多个 CheckBox 控件,则 CheckBoxList 控件是可选择使用控件,它提供方便的数据绑定功能。然而,单个的 CheckBox 控件可提供对布局的更多控制。

警告

此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在您的应用程序中显示从客户端发送来的信息之前,先检查这些信息中是否有可执行脚本、SQL 语句或其他代码。可以在将输入文本显示在控件中之前使用验证控件验证用户输入。ASP.NET 提供了输入请求验证功能,可以阻止用户输入中的脚本和 HTML 代码。有关更多信息,请参见 保证标准控件的安全如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入在 ASP.NET 网页中验证用户输入

辅助功能

默认情况下,为此控件呈现的标记可能不符合辅助功能标准,例如 Web 内容辅助功能准则 1.0 (WCAG) 优先级 1 准则。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

主题 位置
演练:验证 Web 窗体页中的用户输入 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 CheckBox Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:验证 Web 窗体页中的用户输入 使用 Visual Web Developer 生成应用程序
如何:向 Web 窗体页添加 CheckBox Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式获取和设置 CheckBox Web 服务器控件值 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:自定义 ASP.NET CreateUserWizard 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应 CheckBox Web 服务器控件中的用户选择 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 CheckBox Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式获取和设置 CheckBox Web 服务器控件值 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:自定义 ASP.NET CreateUserWizard 控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应 CheckBox Web 服务器控件中的用户选择 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 CheckBoxList Web 服务器控件 生成 ASP .NET Web 应用程序
如何:以编程方式获取和设置 CheckBox Web 服务器控件值 生成 ASP .NET Web 应用程序
如何:自定义 ASP.NET CreateUserWizard 控件 生成 ASP .NET Web 应用程序
如何:在 ASP.NET Web 服务器控件上设置焦点 生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 CheckBox Web 服务器控件 生成 ASP .NET Web 应用程序
如何:响应 CheckBox Web 服务器控件中的用户选择 生成 ASP .NET Web 应用程序

示例

下面的示例展示如何使用 CheckBox 控件来指示在销售总额计算中是否包括税款。

提示

下面的代码示例使用单文件代码模型,当它直接复制到代码隐藏文件时可能不能正常工作。此代码示例必须被复制到具有 .aspx 扩展名的空文本文件中。有关 Web 窗体代码模型的更多信息,请参见 ASP.NET 网页代码模型

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>
 
   <script runat="server">
 
      Sub Check_Clicked(sender As Object, e As EventArgs) 

         ' Calculate the subtotal and display the result in currency format.
         ' Include tax if the check box is selected.
         Message.Text = CalculateTotal(checkbox1.Checked).ToString("c")

      End Sub

      Sub Page_Load(sender As Object, e As EventArgs)

         ' Display the subtotal without tax when the page is first loaded.
         If Not IsPostBack Then

            ' Calculate the subtotal and display the result in currency format.
            Message.Text = CalculateTotal(false).ToString("c")

         End If

      End Sub

      Function CalculateTotal(Taxable As Boolean) As Double 

         ' Calculate the subtotal for the example.
         Dim Result As Double = 1.99 + 2.99 + 3.99

         ' Add tax, if applicable.
         If(Taxable)

            Result += Result * 0.086
         
         End If

         Return Result 

      End Function
 
   </script>
 
</head>
 
<body>
 
   <form runat="server">
 
      <h3>CheckBox CheckedChanged Example</h3>

      Select whether to include tax in the subtotal.

      <br><br>

      <table border="1" cellpadding="5">

         <tr>

            <th colspan="2">

               Shopping cart

            </th>

         </tr>

         <tr>

            <td>

               Item 1

            </td>

            <td>

               $1.99

            </td>

         </tr>

         <tr>

            <td>

               Item 2

            </td>

            <td>

               $2.99

            </td>

         </tr>

         <tr>

            <td>

               Item 3

            </td>

            <td>

               $3.99

            </td>

         </tr>

         <tr>

            <td>

               <b>Subtotal</b>

            </td>

            <td>

               <asp:Label id="Message" runat="server"/>

            </td>

         </tr>

         <tr>

            <td colspan="2">

               <asp:CheckBox id="checkbox1" runat="server"
                    AutoPostBack="True"
                    Text="Include 8.6% sales tax"
                    TextAlign="Right"
                    OnCheckedChanged="Check_Clicked"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>
 
   <script runat="server">
 
      void Check_Clicked(Object sender, EventArgs e) 
      {

         // Calculate the subtotal and display the result in currency format.
         // Include tax if the check box is selected.
         Message.Text = CalculateTotal(checkbox1.Checked).ToString("c");

      }

      void Page_Load(Object sender, EventArgs e)
      {

         // Display the subtotal without tax when the page is first loaded.
         if(!IsPostBack)
         {

            // Calculate the subtotal and display the result in currency format.
            Message.Text = CalculateTotal(false).ToString("c");

         }

      }

      double CalculateTotal(bool Taxable)
      {

         // Calculate the subtotal for the example.
         double Result = 1.99 + 2.99 + 3.99;

         // Add tax, if applicable.
         if(Taxable)
         {
            Result += Result * 0.086;
         }

         return Result; 

      }
 
   </script>
 
</head>
 
<body>
 
   <form runat="server">
 
      <h3>CheckBox CheckedChanged Example</h3>

      Select whether to include tax in the subtotal.

      <br><br>

      <table border="1" cellpadding="5">

         <tr>

            <th colspan="2">

               Shopping cart

            </th>

         </tr>

         <tr>

            <td>

               Item 1

            </td>

            <td>

               $1.99

            </td>

         </tr>

         <tr>

            <td>

               Item 2

            </td>

            <td>

               $2.99

            </td>

         </tr>

         <tr>

            <td>

               Item 3

            </td>

            <td>

               $3.99

            </td>

         </tr>

         <tr>

            <td>

               <b>Subtotal</b>

            </td>

            <td>

               <asp:Label id="Message" runat="server"/>

            </td>

         </tr>

         <tr>

            <td colspan="2">

               <asp:CheckBox id="checkbox1" runat="server"
                    AutoPostBack="True"
                    Text="Include 8.6% sales tax"
                    TextAlign="Right"
                    OnCheckedChanged="Check_Clicked"/>

            </td>

         </tr>

      </table>
                   
   </form>
         
</body>

</html>

.NET Framework 安全性

继承层次结构

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.CheckBox
         System.Web.UI.WebControls.RadioButton

线程安全

此类型的任何公共静态(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

请参见

参考

CheckBox 成员
System.Web.UI.WebControls 命名空间
CheckBoxList
WebControl

其他资源

CheckBox 和 CheckBoxList Web 服务器控件