Partilhar via


HtmlInputRadioButton Server Control Declarative Syntax 

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"
    EnableViewState="False|True"
    Id="string"
    Visible="False|True"
    OnDataBinding="OnDataBinding event handler"
    OnDisposed="OnDisposed event handler"
    OnInit="OnInit event handler"
    OnLoad="OnLoad event handler"
    OnPreRender="OnPreRender event handler"
    OnServerChange="OnServerChange event handler"
    OnUnload="OnUnload event handler"
    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.

NoteNote

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.

NoteNote

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

<!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 Control</title>

   <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 id="Form1" 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
      <br />
      <span id="Span1" runat="server" />
      <br />
      <input type="submit" id="Button1" 
             value="Enter" 
             runat="server" />
   </form>
</body>
</html>
<%@ 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 Control</title>

   <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 id="Form1" 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
      <br />
      <span id="Span1" runat="server" />
      <br />
      <input type="submit" id="Button1" 
             value="Enter" 
             runat="server" />
   </form>
</body>
</html>

See Also

Reference

HtmlInputRadioButton
System.Web.UI.HtmlControls

Other Resources

HTML Server Controls