ASP.Net/Asp Control/RadioButton

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

asp:radiobutton: group name (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Displaying a Basic RadioButton Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Select your favorite Color:<BR>
<asp:radiobutton 
    id="rdoColorBlue"
    text="Blue" 
    font-size="12pt"
    font-name="Comic Sans MS"
    groupname="rgColors"
    runat="server" 
/>
<BR>
<asp:radiobutton 
    id="rdoColorGreen"
    text="Green" 
    font-size="12pt"
    font-name="Comic Sans MS"
    groupname="rgColors"
    checked="True"
    textalign="Left"
    runat="server" 
/>
<BR>
<asp:radiobutton 
    id="rdoColorYellow"
    text="Yellow" 
    font-size="12pt"
    font-name="Comic Sans MS"
    groupname="rgColors"
    textalign="Right"
    runat="server" 
/>
</form>
</BODY>
</HTML>



asp:radiobutton: text align (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Displaying a Basic RadioButton Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
Select your favorite Color:<BR>
<asp:radiobutton 
    id="rdoColorBlue"
    text="Blue" 
    font-size="12pt"
    font-name="Comic Sans MS"
    groupname="rgColors"
    runat="server" 
/>
<BR>
<asp:radiobutton 
    id="rdoColorGreen"
    text="Green" 
    font-size="12pt"
    font-name="Comic Sans MS"
    groupname="rgColors"
    checked="True"
    textalign="Left"
    runat="server" 
/>
<BR>
<asp:radiobutton 
    id="rdoColorYellow"
    text="Yellow" 
    font-size="12pt"
    font-name="Comic Sans MS"
    groupname="rgColors"
    textalign="Right"
    runat="server" 
/>
</form>
</BODY>
</HTML>



Check a asp:radiobutton (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
 
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>RadioButton Controls Sample Page</TITLE>
</HEAD>
<BODY>
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:Label 
    id="lblMessage" 
    Font-Size="12pt"
    Font-Bold="True"
    Font-Name="Lucida Console"
    Text="What is your favorite color?"
    runat="server"
/>
<BR><BR>
<asp:RadioButton 
    id="rdoColorRed"
    Text="Red" 
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgColor"
    Checked="True"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoColorBlue"
    Text="Blue" 
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgColor"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoColorGreen"
    Text="Green" 
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgColor"
    runat="server" 
/>
<BR><BR>
<asp:Label 
    id="lblMessage2" 
    Font-Size="12pt"
    Font-Bold="True"
    Font-Name="Lucida Console"
    Text="What is your favorite day?"
    runat="server"
/>
<BR><BR>
<asp:RadioButton 
    id="rdoDayMonday"
    Text="Monday" 
    TextAlign="Left"
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgDay"
    Checked="True"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoDayTuesday"
    Text="Tuesday" 
    TextAlign="Left"
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgDay"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoDayWednesday"
    Text="Wednesday" 
    TextAlign="Left"
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgDay"
    runat="server" 
/>
</Font>
</Form>
</BODY>
</HTML>



Font-Size, Font-Bold, Font-Name for radio button (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
 
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>RadioButton Controls Sample Page</TITLE>
</HEAD>
<BODY>
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:Label 
    id="lblMessage" 
    Font-Size="12pt"
    Font-Bold="True"
    Font-Name="Lucida Console"
    Text="What is your favorite color?"
    runat="server"
/>
<BR><BR>
<asp:RadioButton 
    id="rdoColorRed"
    Text="Red" 
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgColor"
    Checked="True"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoColorBlue"
    Text="Blue" 
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgColor"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoColorGreen"
    Text="Green" 
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgColor"
    runat="server" 
/>
<BR><BR>
<asp:Label 
    id="lblMessage2" 
    Font-Size="12pt"
    Font-Bold="True"
    Font-Name="Lucida Console"
    Text="What is your favorite day?"
    runat="server"
/>
<BR><BR>
<asp:RadioButton 
    id="rdoDayMonday"
    Text="Monday" 
    TextAlign="Left"
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgDay"
    Checked="True"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoDayTuesday"
    Text="Tuesday" 
    TextAlign="Left"
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgDay"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoDayWednesday"
    Text="Wednesday" 
    TextAlign="Left"
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgDay"
    runat="server" 
/>
</Font>
</Form>
</BODY>
</HTML>



Get selected RadioButton (C#)

<%@ Page Language="C#" %>
<script runat="server">
    void Page_Load()
     {
     if (Page.IsPostBack)
      Message.Text = "You have selected " + radCity.SelectedItem.Value;
    }
</script>
<html>
<head>
    <title>Radio Button List Example</title>
</head>
<body>
    <asp:Label id="Message" runat="server"></asp:Label>
    <br />
    <br />
    Which city interests you? 
    <br />
    <br />
    <form runat="server">
        <asp:radiobuttonlist id="radCity" runat="server">
            <asp:listitem id="optA" runat="server" value="A" />
            <asp:listitem id="optB" runat="server" value="B" />
            <asp:listitem id="optC" runat="server" value="C" />
        </asp:radiobuttonlist>
        <br />
        <input type="submit" value="Submit Query" />
    </form>
</body>
</html>



GroupName for asp:radiobutton (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
 
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>RadioButton Controls Sample Page</TITLE>
</HEAD>
<BODY>
<form runat="server">
<Font Face="Tahoma" Size="+1">
<asp:Label 
    id="lblMessage" 
    Font-Size="12pt"
    Font-Bold="True"
    Font-Name="Lucida Console"
    Text="What is your favorite color?"
    runat="server"
/>
<BR><BR>
<asp:RadioButton 
    id="rdoColorRed"
    Text="Red" 
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgColor"
    Checked="True"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoColorBlue"
    Text="Blue" 
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgColor"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoColorGreen"
    Text="Green" 
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgColor"
    runat="server" 
/>
<BR><BR>
<asp:Label 
    id="lblMessage2" 
    Font-Size="12pt"
    Font-Bold="True"
    Font-Name="Lucida Console"
    Text="What is your favorite day?"
    runat="server"
/>
<BR><BR>
<asp:RadioButton 
    id="rdoDayMonday"
    Text="Monday" 
    TextAlign="Left"
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgDay"
    Checked="True"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoDayTuesday"
    Text="Tuesday" 
    TextAlign="Left"
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgDay"
    runat="server" 
/>
<asp:RadioButton 
    id="rdoDayWednesday"
    Text="Wednesday" 
    TextAlign="Left"
    Font-Size="12pt"
    Font-Name="Comic Sans MS"
    GroupName="rgDay"
    runat="server" 
/>
</Font>
</Form>
</BODY>
</HTML>



On asp:radiobutton selection state changed (VB.net)

<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub rgShipping_Clicked(Sender As Object, E As EventArgs)
    If rdoUPS.Checked = True Then
        Session("ShippingMethod") = "UPS"
    ElseIf rdoUSPS.Checked = True Then
        Session("ShippingMethod") = "USPS"
    Else
        Session("ShippingMethod") = "FedEx"
    End If
    lblShipSelected.Text = Session("ShippingMethod")
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Writing Code that Fires when Selection Changes</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
<asp:Label 
    id="lblShipSelected" 
    runat="server"
/>
<BR>
Select Shipping Type:
<BR>
<asp:radiobutton 
    id="rdoUPS"
    text="UPS" 
    autopostback="True"
    oncheckedchanged="rgShipping_Clicked"
    groupname="rgShipping"
    runat="server" 
/>
<BR>
<asp:radiobutton 
    id="rdoUSPS"
    text="United States Postal Service" 
    autopostback="True"
    oncheckedchanged="rgShipping_Clicked"
    groupname="rgShipping"
    runat="server" 
/>
<BR>
<asp:radiobutton 
    id="rdoFedEx"
    text="FedEx" 
    autopostback="True"
    oncheckedchanged="rgShipping_Clicked"
    groupname="rgShipping"
    runat="server" 
/>
<BR>
</form>
</BODY>
</HTML>



Radio button auto post back (C#)

<%@ Page Language="C#" %>
<script runat="server">
    protected void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton selectedRadioButton = (RadioButton)sender;
        lblResult.Text = selectedRadioButton.Text;
    }
</script>
<html>
<head>
    <title>RadioButton AutoPostBack</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    How did you hear about our Website?
    
    <ul>
        <li>
        <asp:RadioButton
            id="rdlMagazine"
            Text="Magazine Article"
            GroupName="Source"
            AutoPostBack="true"
            OnCheckedChanged="RadioButton_CheckedChanged"
            Runat="server" />
        </li>
        <li>
        <asp:RadioButton
            id="rdlTelevision"
            Text="Television Program"
            GroupName="Source"
            AutoPostBack="true"
            OnCheckedChanged="RadioButton_CheckedChanged"
            Runat="server" />
        </li>
        <li>
        <asp:RadioButton
            id="rdlOther"
            Text="Other Source"
            GroupName="Source"
            AutoPostBack="true"
            OnCheckedChanged="RadioButton_CheckedChanged"
            Runat="server" />
        </li>
    </ul>
    <hr />
    
    <asp:Label
        id="lblResult"
        Runat="server" />
    
    </div>
    </form>
</body>
</html>



Read RadioButton selection (C#)

<%@ Page Language="c#" %>
<script runat="server">
    void Page_Load()
    {
      string msg = "You have selected the following items:<br />";
      if (chkCities.Items[0].Selected) 
          msg += chkCities.Items[0].Text + "<br />";
      if (chkCities.Items[1].Selected) 
          msg += chkCities.Items[1].Text + "<br />";
      if (chkCities.Items[2].Selected) 
          msg += chkCities.Items[2].Text + "<br />";
          
      lblCities.Text = msg;
    }
</script>
<html>
<head>
    <title>Check Box List Example</title>
</head>
<body>
    <asp:Label id="lblCities" runat="server"></asp:Label>
    <br />
    <br />
    Which city do you wish to look at hotels for?<br />
    <br />
    <form runat="server">
        <asp:checkboxlist id="chkCities" runat="server">
            <asp:listitem id="optMadrid" runat="server" value="Madrid" />
            <asp:listitem id="optOslo" runat="server" value="Oslo" />
            <asp:listitem id="optLisbon" runat="server" value="Lisbon" />
        </asp:checkboxlist>
        <br />
        <br />
        <input type="submit" value="Submit Query" />
    </form>
</body>
</html>



Set asp:radiobutton checked (VB.net)

<%@ Page Language="vb" %>
<html>
<head>
   <title>Selection Control Example</title>
   <script runat="server">
      Sub Page_Load()
         MyCheckBox1.Checked = True
         MyRadioButton1.Checked = False
         MyListBox.SelectionMode = ListSelectionMode.Multiple
         MyDropDownList.SelectedIndex = 1
         MyCheckBoxList.RepeatDirection = RepeatDirection.Horizontal
         MyRadioButtonList.RepeatLayout = RepeatLayout.Table
      End Sub
   </script>
</head>
<body>
   <h1>Selection Control Example</h1>
   <form runat="server">
      <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               CheckBox Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:checkbox id="MyCheckBox1" 
                  text="Vanilla" runat="server" />
               <asp:checkbox id="MyCheckBox2" 
                  text="Chocolate" runat="server" />
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               RadioButton Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:radiobutton id="MyRadioButton1" groupname="Group1" 
                  checked=True text="Yes" runat="Server"/>
               <asp:radiobutton id="MyRadioButton2" groupname="Group1"
                  text="No" runat="Server"/>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               ListBox Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:listbox id="MyListBox" runat="server">
                  <asp:listitem value="Vanilla" selected="true">Vanilla</asp:listitem>
                  <asp:listitem value="Chocolate">Chocolate</asp:listitem>
                  <asp:listitem value="Strawberry">Strawberry</asp:listitem>
               </asp:listbox>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               DropDownList Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:dropdownlist id="MyDropDownList" runat="server">
                  <asp:listitem value="Single" selected="true">Single</asp:listitem>
                  <asp:listitem value="Multiline">Multiline</asp:listitem>
                  <asp:listitem value="Password">Password</asp:listitem>
               </asp:dropdownlist>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               CheckBoxList Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:checkboxlist id="MyCheckBoxList"
                  repeatdirection="vertical" runat="server">
                  <asp:listitem value="Vanilla" text="Vanilla"/>
                  <asp:listitem value="Chocolate" text="Chocolate"/>
                  <asp:listitem value="Strawberry" text="Strawberry"/>
               </asp:checkboxlist>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               RadioButtonList Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:radiobuttonlist id="MyRadioButtonList" repeatdirection="Horizontal" runat="server">
                  <asp:listitem value="Female" text="Female" selected="true"/>
                  <asp:listitem value="Male" text="Male"/>
               </asp:radiobuttonlist>
            </asp:tablecell>
         </asp:tablerow>
      </asp:table>
   </form>
</body>
</html>



Use "Switch" to read Radio Button Choice (C#)

<script language="C#" runat="server">
void Page_Load()
{
  if (Page.IsPostBack) {
    switch(Destination.SelectedItem.Value) 
    {
      case "B":
        Message.Text = "You selected B";
        break; 
      case "C":
        Message.Text = "You selected C";
        break; 
      case "D": 
        Message.Text = "Your selected D";
        break; 
      default:
        Message.Text = "you did not select a destination";
        break; 
    }
  }
}
</script>
<html>
<head></head>
<body>
  <form runat="server">
  Select your choice of destination:
  <br><br>
  <asp:radiobuttonlist id="Destination" runat="server">
    <asp:listitem>B</asp:listitem>
    <asp:listitem>C</asp:listitem>
    <asp:listitem>D</asp:listitem>
  </asp:radiobuttonlist>
  <br><br>
  <input type="submit" value="Submit Choice">
  <br><br>
  <asp:label id="Message" runat="server"/>
</form>
</body>
</html>