Editar

Compartilhar via


HtmlInputRadioButton.Checked Property

Definition

Gets or sets a value indicating whether the HtmlInputRadioButton control is selected.

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

Property Value

true if the HtmlInputRadioButton control is selected; otherwise, false.

Examples

The following code example demonstrates how to use the Checked property to determine which radio button in the group is selected.

<%@ 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>

Remarks

Use the Checked property to determine whether the HtmlInputRadioButton control is selected. If you have a group of HtmlInputRadioButton controls, you must iterate through the controls and test the Checked property of each control individually. You can also use this property to programmatically specify whether the control is selected.

You can group HtmlInputRadioButton controls together by specifying a common value for the Name property of each radio button control you want to include in the group.

Note

When you group HtmlInputRadioButton controls, only one radio button in the group can be selected at a time. The Checked property of the selected control is set to true, while the same property is set to false for all other radio buttons in the group.

Applies to

See also