다음을 통해 공유


HtmlInputRadioButton Control

Creates a server-side control that maps to the <input type=radio> HTML element and allows you to create a radio button on a Web page.

<input type=radio
       id="programmaticID"
       checked
       name="radiobuttongroup"   
       runat="server" >

Remarks

Use the HtmlInputRadioButton control to program against the HTML <input type=radio> element. You can group multiple HtmlInputRadioButton controls together by setting the Name property to a value that is common to all <input type=radio> elements within the group. Radio buttons in the same group are mutually exclusive; only one radio button in the group can be selected at a time.

Note   This control does not require a closing tag.

The HtmlRadioButton control does not automatically post back to the server. You must rely on using one of the button controls, such as the HtmlInputButton, HtmlInputImage, or HtmlButton, to post back to the server. You can program against the HtmlRadioButton control by writing a handler for the ServerChange event.

Note   The ServerChange event is only raised for radio buttons that change to a checked state.

Example

The following example demonstrates how to create an event handler for the ServerChange event of the HtmlRadioButton control. The event handler determines which radio button is selected and displays the selection in a message.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub Server_Change(Source As Object, e As EventArgs)
         If Radio1.Checked = True Then
            Span1.InnerHtml = "Radio1 is checked"
         Else
            If Radio2.Checked = True Then
               Span1.InnerHtml = "Radio2 is checked"
            Else
               If Radio3.Checked = True Then
                  Span1.InnerHtml = "Radio3 is checked"
               End If
            End If
         End If
      End Sub
   </script>
</head>
<body>
   <form runat="server">
      <h3>HtmlInputRadioButton Sample</h3>
      <input type="radio" 
             id="Radio1" 
             name="Mode"
             OnServerChange="Server_Change" 
             runat="server"/>
      Option 1<br>
      <input type="radio" 
             id="Radio2" 
             name="Mode"
             OnServerChange="Server_Change" 
             runat="server"/>
      Option 2<br>
      <input type="radio" 
             id="Radio3" 
             name="Mode"
             OnServerChange="Server_Change" 
             runat="server"/>
      Option 3
      <p>
      <span id=Span1 runat="server" />
      <p>
      <input type=submit id="Button1" 
             value="Enter" 
             runat="server">
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void Server_Change(object Source, EventArgs e) 
      {
         if (Radio1.Checked == true)
            Span1.InnerHtml = "Radio1 is checked";
         else if (Radio2.Checked == true)
            Span1.InnerHtml = "Radio2 is checked";
         else if (Radio3.Checked == true)
            Span1.InnerHtml = "Radio3 is checked";
      }
   </script>
</head>
<body>
   <form runat="server">
      <h3>HtmlInputRadioButton Sample</h3>
      <input type="radio" 
             id="Radio1" 
             name="Mode"
             OnServerChange="Server_Change" 
             runat="server"/>
      Option 1<br>
      <input type="radio" 
             id="Radio2" 
             name="Mode"
             OnServerChange="Server_Change" 
             runat="server"/>
      Option 2<br>
      <input type="radio" 
             id="Radio3" 
             name="Mode"
             OnServerChange="Server_Change" 
             runat="server"/>
      Option 3
      <p>
      <span id=Span1 runat="server" />
      <p>
      <input type=submit id="Button1" 
             value="Enter" 
             runat="server">
   </form>
</body>
</html>

See Also

ASP.NET Syntax for HTML Controls | HtmlInputRadioButton Class | System.Web.UI.HtmlControls Namespace