ASP.Net/Asp Control/RadioButton

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

asp:radiobutton: group name (VB.net)

   <source lang="csharp">

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

Select your favorite Color:
<asp:radiobutton

   id="rdoColorBlue"
   text="Blue" 
   font-size="12pt"
   font-name="Comic Sans MS"
   groupname="rgColors"
   runat="server" 

/>
<asp:radiobutton

   id="rdoColorGreen"
   text="Green" 
   font-size="12pt"
   font-name="Comic Sans MS"
   groupname="rgColors"
   checked="True"
   textalign="Left"
   runat="server" 

/>
<asp:radiobutton

   id="rdoColorYellow"
   text="Yellow" 
   font-size="12pt"
   font-name="Comic Sans MS"
   groupname="rgColors"
   textalign="Right"
   runat="server" 

/> </form> </BODY> </HTML>

      </source>
   
  


asp:radiobutton: text align (VB.net)

   <source lang="csharp">

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

Select your favorite Color:
<asp:radiobutton

   id="rdoColorBlue"
   text="Blue" 
   font-size="12pt"
   font-name="Comic Sans MS"
   groupname="rgColors"
   runat="server" 

/>
<asp:radiobutton

   id="rdoColorGreen"
   text="Green" 
   font-size="12pt"
   font-name="Comic Sans MS"
   groupname="rgColors"
   checked="True"
   textalign="Left"
   runat="server" 

/>
<asp:radiobutton

   id="rdoColorYellow"
   text="Yellow" 
   font-size="12pt"
   font-name="Comic Sans MS"
   groupname="rgColors"
   textalign="Right"
   runat="server" 

/> </form> </BODY> </HTML>

      </source>
   
  


Check a asp:radiobutton (VB.net)

   <source lang="csharp">

<%@ 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"> <asp:Label

   id="lblMessage" 
   Font-Size="12pt"
   Font-Bold="True"
   Font-Name="Lucida Console"
   Text="What is your favorite color?"
   runat="server"

/>

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

/>

<asp:Label

   id="lblMessage2" 
   Font-Size="12pt"
   Font-Bold="True"
   Font-Name="Lucida Console"
   Text="What is your favorite day?"
   runat="server"

/>

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

/> </Form> </BODY> </HTML>

      </source>
   
  


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

   <source lang="csharp">

<%@ 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"> <asp:Label

   id="lblMessage" 
   Font-Size="12pt"
   Font-Bold="True"
   Font-Name="Lucida Console"
   Text="What is your favorite color?"
   runat="server"

/>

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

/>

<asp:Label

   id="lblMessage2" 
   Font-Size="12pt"
   Font-Bold="True"
   Font-Name="Lucida Console"
   Text="What is your favorite day?"
   runat="server"

/>

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

/> </Form> </BODY> </HTML>

      </source>
   
  


Get selected RadioButton (C#)

   <source lang="csharp">

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

Which city interests you?

<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>
<input type="submit" value="Submit Query" /> </form>

</body> </html>

      </source>
   
  


GroupName for asp:radiobutton (VB.net)

   <source lang="csharp">

<%@ 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"> <asp:Label

   id="lblMessage" 
   Font-Size="12pt"
   Font-Bold="True"
   Font-Name="Lucida Console"
   Text="What is your favorite color?"
   runat="server"

/>

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

/>

<asp:Label

   id="lblMessage2" 
   Font-Size="12pt"
   Font-Bold="True"
   Font-Name="Lucida Console"
   Text="What is your favorite day?"
   runat="server"

/>

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

/> </Form> </BODY> </HTML>

      </source>
   
  


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

   <source lang="csharp">

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

<asp:Label

   id="lblShipSelected" 
   runat="server"

/>
Select Shipping Type:
<asp:radiobutton

   id="rdoUPS"
   text="UPS" 
   autopostback="True"
   oncheckedchanged="rgShipping_Clicked"
   groupname="rgShipping"
   runat="server" 

/>
<asp:radiobutton

   id="rdoUSPS"
   text="United States Postal Service" 
   autopostback="True"
   oncheckedchanged="rgShipping_Clicked"
   groupname="rgShipping"
   runat="server" 

/>
<asp:radiobutton

   id="rdoFedEx"
   text="FedEx" 
   autopostback="True"
   oncheckedchanged="rgShipping_Clicked"
   groupname="rgShipping"
   runat="server" 

/>
</form> </BODY> </HTML>

      </source>
   
  


Radio button auto post back (C#)

   <source lang="csharp">

<%@ 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">
   How did you hear about our Website?
   
  • <asp:RadioButton id="rdlMagazine" Text="Magazine Article" GroupName="Source" AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" Runat="server" />
  • <asp:RadioButton id="rdlTelevision" Text="Television Program" GroupName="Source" AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" Runat="server" />
  • <asp:RadioButton id="rdlOther" Text="Other Source" GroupName="Source" AutoPostBack="true" OnCheckedChanged="RadioButton_CheckedChanged" Runat="server" />

   <asp:Label
       id="lblResult"
       Runat="server" />
   
   </form>

</body> </html>

      </source>
   
  


Read RadioButton selection (C#)

   <source lang="csharp">

<%@ Page Language="c#" %> <script runat="server">

   void Page_Load()
   {
     string msg = "You have selected the following items:
"; if (chkCities.Items[0].Selected) msg += chkCities.Items[0].Text + "
"; if (chkCities.Items[1].Selected) msg += chkCities.Items[1].Text + "
"; if (chkCities.Items[2].Selected) msg += chkCities.Items[2].Text + "
"; lblCities.Text = msg; }

</script> <html> <head>

   <title>Check Box List Example</title>

</head> <body>

   <asp:Label id="lblCities" runat="server"></asp:Label>
   

Which city do you wish to look at hotels for?

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

<input type="submit" value="Submit Query" /> </form>

</body> </html>

      </source>
   
  


Set asp:radiobutton checked (VB.net)

   <source lang="csharp">

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

Selection Control Example

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

      </source>
   
  


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

   <source lang="csharp">

<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:
 

<asp:radiobuttonlist id="Destination" runat="server"> <asp:listitem>B</asp:listitem> <asp:listitem>C</asp:listitem> <asp:listitem>D</asp:listitem> </asp:radiobuttonlist>

<input type="submit" value="Submit Choice">

<asp:label id="Message" runat="server"/>

</form> </body> </html>

      </source>