ASP.Net/Asp Control/Radio Button List

Материал из .Net Framework эксперт
Версия от 14:52, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add ListItem to RadioButtonList

   <source lang="csharp">

<script language="C#" runat="server"> protected void Page_Load(object o, EventArgs e) {

   if(!IsPostBack) {
       ListItem item;
       item = new ListItem("A", "1");
       languageRadioButtonList.Items.Add(item);
       item = new ListItem("B", "2");
       languageRadioButtonList.Items.Add(item);
       item = new ListItem("C", "3");
       languageRadioButtonList.Items.Add(item);
   }

} protected void DropDownListSelectionChanged(object o, EventArgs e) {

   favoriteLanguage.Text = languageRadioButtonList.SelectedItem.Value;

} </script> <form runat="server"> <asp:RadioButtonList

   id="languageRadioButtonList"
   runat="server" 
   OnSelectedIndexChanged="DropDownListSelectionChanged" >

</asp:RadioButtonList>
Favorite Language: <asp:label runat="server" id="favoriteLanguage" style="color:blue" />
<asp:button runat="server" Text="Submit"/> </form>

</source>
   
  


Get selected item, value and text from asp:RadioButtonList (VB.net)

   <source lang="csharp">

<%@ Page Language=VB Debug=true %> <script runat=server> Sub SubmitBtn_Click(Sender As Object, E As EventArgs)

   lblMessage.Text = "Selected Text: " _
       & rbl1.SelectedItem.Text & "
Selected Value: " _ & rbl1.SelectedItem.Value & "
Selected Index: " _ & rbl1.SelectedIndex

End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Creating a Basic RadioButtonList Control</TITLE> </HEAD> <form runat="server">

<asp:Label

   id="lblMessage"
   runat="server"

/>

<asp:RadioButtonList

   id="rbl1" 
   runat="server"
   cellpadding="5"
   cellspacing="5"
   repeatcolumns="3"
   repeatdirection="Vertical"
   repeatlayout="Table"
   textalign="Right"

>

   <asp:ListItem value="12">Blue</asp:ListItem>
   <asp:ListItem value="11">Red</asp:ListItem>
   <asp:ListItem value="2">Green</asp:ListItem>
   <asp:ListItem value="32">Purple</asp:ListItem>
   <asp:ListItem value="8">Black</asp:ListItem>
   <asp:ListItem value="15" Selected>Gold</asp:ListItem>

</asp:RadioButtonList>

<asp:button

   id="butOK"
   text="OK"
   type="Submit"
   onclick="SubmitBtn_Click" 
   runat="server"

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

      </source>
   
  


Get selected item value from asp:radiobuttonlist (VB.net)

   <source lang="csharp">

<script runat="server" language="vb">

 Sub Page_Load()
  if Page.IsPostBack then
   Message.Text = "You have selected the following: " + radio1.SelectedItem.Value
  end if
 End Sub

</script> <html>

 <head>
   <title>Radio Button List Example</title>
 </head>
 <body>
   <asp:label id="Message" runat="server" />
   

Which city do you wish to look at hotels for?

<form runat="server"> <asp:radiobuttonlist id="radio1" runat="server"> <asp:listitem id="option1" runat="server" value="Madrid" /> <asp:listitem id="option2" runat="server" value="Oslo" /> <asp:listitem id="option3" runat="server" value="Lisbon" /> </asp:radiobuttonlist>

<input type="Submit"> </form> </body>

</html>

      </source>
   
  


Get selected radio button from radio button list (C#)

   <source lang="csharp">

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

   protected void btnSubmit_Click(object sender, EventArgs e)
   {
       if (rdlMagazine.Checked)
           lblResult.Text = rdlMagazine.Text;
       if (rdlTelevision.Checked)
           lblResult.Text = rdlTelevision.Text;
       if (rdlOther.Checked)
           lblResult.Text = rdlOther.Text;
   }

</script> <html> <head id="Head1" runat="server">

   <title>Show RadioButton</title>

</head> <body>

   <form id="form1" runat="server">
   How did you hear about our Website?
   
  • <asp:RadioButton id="rdlMagazine" Text="Magazine Article" GroupName="Source" Runat="server" />
  • <asp:RadioButton id="rdlTelevision" Text="Television Program" GroupName="Source" Runat="server" />
  • <asp:RadioButton id="rdlOther" Text="Other Source" GroupName="Source" Runat="server" />
   <asp:Button
       id="btnSubmit"
       Text="Submit"
       Runat="server" OnClick="btnSubmit_Click" />
  

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

</body> </html>

      </source>
   
  


Get selected value from asp:radiobuttonlist (C#)

   <source lang="csharp">

<script runat="server" language="C#">

 void Page_Load()
 { 
   if (Page.IsPostBack)
    { 
       Message.Text = "You have selected the following: " + radio1.SelectedItem.Value;
    }
 }

</script> <html>

 <head>
   <title>Radio Button List Example</title>
 </head>
 <body>
   <asp:label id="Message" runat="server" />
   

Which city do you wish to look at hotels for?

<form runat="server"> <asp:radiobuttonlist id="radio1" runat="server"> <asp:listitem id="option1" runat="server" value="Madrid" /> <asp:listitem id="option2" runat="server" value="Oslo" /> <asp:listitem id="option3" runat="server" value="Lisbon" /> </asp:radiobuttonlist>

<input type="Submit"> </form> </body>

</html>

      </source>
   
  


On radiobutton list changed (VB.net)

   <source lang="csharp">

<%@ Page Language=VB Debug=true %> <script runat=server> Sub rbl_Changed(Sender As Object, E As EventArgs)

   Dim TheAlert as String
   TheAlert = "<SCRIPT LANGUAGE=""JavaScript"">" & Chr(13) _
       & "<" & "/" & "SCRIPT>"
   Response.Write(TheAlert)

End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Writing Code that Fires When an Item is Selected in a RadioButtonList Control</TITLE> </HEAD> <form runat="server">

<asp:RadioButtonList

   id="rbl1" 
   runat="server"
   autopostback="True"
   onselectedindexchanged="rbl_Changed"

>

   <asp:ListItem value="12">Blue</asp:ListItem>
   <asp:ListItem value="11">Red</asp:ListItem>
   <asp:ListItem value="2">Green</asp:ListItem>

</asp:RadioButtonList>

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

      </source>
   
  


RadioButtonList Control

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>RadioButtonList Control</title>

</head> <body>

   <form id="form1" runat="server">
   <asp:Label ID="lblTime" runat="server" OnInit="lblTime_Init"></asp:Label>
     <asp:radioButtonList
        id="rblSize" runat="server" 
        autoPostBack="true"
        cellSpacing="20"
        repeatColumns="3"
        repeatDirection="horizontal"
        RepeatLayout="table"
        textAlign="right"
        OnSelectedIndexChanged="rblSize_SelectedIndexChanged">
        <asp:ListItem text="10pt" value="10"/>  
        <asp:ListItem text="14pt" value="14"/>  
        <asp:ListItem text="16pt" value="16"/>  
     </asp:radioButtonList>
   </form>

</body> </html> File: Default.aspx.cs using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class Default_aspx : System.Web.UI.Page {

  protected void lblTime_Init(object sender, EventArgs e)
  {
    lblTime.Font.Name = "Verdana";
    lblTime.Font.Size = 20;
    lblTime.Font.Bold = true;
    lblTime.Font.Italic = true;
    lblTime.Text = DateTime.Now.ToString();
  }
  protected void rblSize_SelectedIndexChanged(object sender, EventArgs e)
  {
    if (rblSize.SelectedIndex != -1)
    {
      int size = Convert.ToInt32(rblSize.SelectedItem.Value);
      lblTime.Font.Size = size;
    }
  }
}
</source>
   
  


Select case for asp:radiobuttonlist (VB.net)

   <source lang="csharp">

<script language="vb" runat="server"> Sub Page_Load()

 If Page.IsPostBack Then
   Select Case(Destination.SelectedItem.Value)
     Case "Barcelona":
       Message.Text = "Barcelona"
     Case "Oslo":
       Message.Text = "Oslo"
     Case "Lisbon": 
       Message.Text = "Lisbon"
     Case else
       Message.Text = "you did not select a destination we travel to"
   End Select
 End If

End Sub </script> <html> <head></head> <body>

 <form runat="server">
 Select your choice of destination:
 

<asp:radiobuttonlist id="destination" runat="server"> <asp:listitem>Barcelona</asp:listitem> <asp:listitem>Oslo</asp:listitem> <asp:listitem>Lisbon</asp:listitem> </asp:radiobuttonlist>

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

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

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

      </source>
   
  


select case statement for asp:radiobuttonlist (C#)

   <source lang="csharp">

<script language="C#" runat="server"> void Page_Load() {

 if (Page.IsPostBack) {
   switch(Destination.SelectedItem.Value) 
   {
     case "Barcelona":
       Message.Text = "You selected Spain"s lively Catalan city";
       break; 
     case "Oslo":
       Message.Text = "Experience the majesty of Norway"s capital city";
       break; 
     case "Lisbon": 
       Message.Text = "Portugal"s famous seaport and cultural hub";
       break; 
     default:
       Message.Text = "you did not select a destination we travel to";
       break; 
   }
 }

} </script> <html> <head></head> <body>

 <form runat="server">
 Select your choice of destination:
 

<asp:radiobuttonlist id="Destination" runat="server"> <asp:listitem>Barcelona</asp:listitem> <asp:listitem>Oslo</asp:listitem> <asp:listitem>Lisbon</asp:listitem> </asp:radiobuttonlist>

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

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

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

      </source>
   
  


Set asp:RadioButtonList Repeat Layout (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>
   
  


Setting and Retrieving the Selected Item in a RadioButton Control Group (VB.net)

   <source lang="csharp">

<%@ Page Language=VB Debug=true %> <script runat=server> Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)

   If Not IsPostBack Then
       rdoColorYellow.Checked = True
   End If

End Sub Sub SubmitBtn_Click(Sender As Object, E As EventArgs)

   Dim MyControl as Control
   Dim MyRadioButton as RadioButton
   For Each MyControl in frmMainForm.Controls
       If MyControl.GetType().FullName = "System.Web.UI.WebControls.RadioButton" Then
           MyRadioButton = MyControl
           If MyRadioButton.GroupName = "rgColors" then
               If MyRadioButton.Checked = "True" Then
                   lblColorSelected.Text = "You selected the color " _
                       & MyRadioButton.Text
               End If
           End If
       End If
   Next

End Sub </SCRIPT> <HTML> <HEAD> <TITLE>Setting and Retrieving the Selected Item in a RadioButton Control Group</TITLE> </HEAD> <form

   id="frmMainForm"
   runat="server"

>

<asp:Label

   id="lblColorSelected" 
   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" 

/>

<asp:button

   id="butOK"
   text="OK"
   Type="Submit"
   OnClick="SubmitBtn_Click" 
   runat="server"

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

      </source>