HtmlInputRadioButton.Checked 属性

定义

获取或设置一个值,该值指示是否选择了 HtmlInputRadioButton 控件。

public:
 property bool Checked { bool get(); void set(bool value); };
public bool Checked { get; set; }
member this.Checked : bool with get, set
Public Property Checked As Boolean

属性值

Boolean

如果选择了 HtmlInputRadioButton 控件,则为 true;否则为 false

示例

下面的代码示例演示如何使用 Checked 属性来确定组中的哪个单选按钮处于选中状态。

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>HtmlInputRadioButton Sample</title>
<script language="C#" runat="server">

      void Button1_Click(object sender, EventArgs e) 
      {

         if (Radio1.Checked == true)
            Span1.InnerHtml = "Option 1 is selected";
         else if (Radio2.Checked == true)
            Span1.InnerHtml = "Option 2 is selected";
         else if (Radio3.Checked == true)
            Span1.InnerHtml = "Option 3 is selected";
      }

   </script>

</head>
<body>

   <form id="form1" runat="server">

      <h3>HtmlInputRadioButton Sample</h3>

      <input type="radio" 
             id="Radio1" 
             name="Mode" 
             runat="server"/>

      Option 1<br />

      <input type="radio" 
             id="Radio2" 
             name="Mode" 
             runat="server"/>
      
      Option 2<br />

      <input type="radio" 
             id="Radio3" 
             name="Mode" 
             runat="server"/>

      Option 3

      <br />
      <span id="Span1" runat="server" />

      <br />
      <input type="button" 
             id="Button1" 
             value="Enter" 
             onserverclick="Button1_Click" 
             runat="server" />

   </form>

</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>HtmlInputRadioButton Sample</title>
<script language="VB" runat="server">

    Sub Button1_Click(sender As Object, e As EventArgs)
        
        If Radio1.Checked = True Then
            Span1.InnerHtml = "Option 1 is selected"
        Else
            If Radio2.Checked = True Then
                Span1.InnerHtml = "Option 2 is selected"
            Else
                If Radio3.Checked = True Then
                    Span1.InnerHtml = "Option 3 is selected"
                End If
            End If
        End If
    End Sub 'Button1_Click

   </script>

</head>
<body>

   <form id="form1" runat="server">

      <h3>HtmlInputRadioButton Sample</h3>

      <input type="radio" 
             id="Radio1" 
             name="Mode" 
             runat="server"/>

      Option 1<br />

      <input type="radio" 
             id="Radio2" 
             name="Mode" 
             runat="server"/>
      
      Option 2<br />

      <input type="radio" 
             id="Radio3" 
             name="Mode" 
             runat="server"/>

      Option 3

      <br />
      <span id="Span1" runat="server" />

      <br />
      <input type="button" 
             id="Button1" 
             value="Enter" 
             onserverclick="Button1_Click" 
             runat="server" />

   </form>

</body>
</html>

注解

使用 Checked 属性确定控件是否 HtmlInputRadioButton 处于选中状态。 如果你有一组 HtmlInputRadioButton 控件,则必须循环访问控件并单独测试 Checked 每个控件的属性。 还可以使用此属性以编程方式指定是否选择了控件。

可以通过 HtmlInputRadioButton 指定 Name 要包含在组中的每个单选按钮控件的属性的公用值来组合控件。

备注

对控件进行分组 HtmlInputRadioButton 时,一次只能选择组中的一个单选按钮。 所选 Checked 控件的属性设置为 true,而同一属性设置为 false 组中所有其他单选按钮。

适用于

另请参阅