ASP.Net/Asp Control/Radio Button List
Версия от 15:30, 26 мая 2010; (обсуждение)
Содержание
- 1 Add ListItem to RadioButtonList
- 2 Get selected item, value and text from asp:RadioButtonList (VB.net)
- 3 Get selected item value from asp:radiobuttonlist (VB.net)
- 4 Get selected radio button from radio button list (C#)
- 5 Get selected value from asp:radiobuttonlist (C#)
- 6 On radiobutton list changed (VB.net)
- 7 RadioButtonList Control
- 8 Select case for asp:radiobuttonlist (VB.net)
- 9 select case statement for asp:radiobuttonlist (C#)
- 10 Set asp:RadioButtonList Repeat Layout (VB.net)
- 11 Setting and Retrieving the Selected Item in a RadioButton Control Group (VB.net)
Add ListItem to RadioButtonList
<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><br/>
Favorite Language: <b><asp:label runat="server" id="favoriteLanguage" style="color:blue" /></b><br />
<asp:button runat="server" Text="Submit"/>
</form>
Get selected item, value and text from asp:RadioButtonList (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
lblMessage.Text = "Selected Text: " _
& rbl1.SelectedItem.Text & "<BR>Selected Value: " _
& rbl1.SelectedItem.Value & "<BR>Selected Index: " _
& rbl1.SelectedIndex
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Creating a Basic RadioButtonList Control</TITLE>
</HEAD>
<form runat="server">
<BR><BR>
<asp:Label
id="lblMessage"
runat="server"
/>
<BR><BR>
<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>
<BR><BR>
<asp:button
id="butOK"
text="OK"
type="Submit"
onclick="SubmitBtn_Click"
runat="server"
/>
</form>
</BODY>
</HTML>
Get selected item value from asp:radiobuttonlist (VB.net)
<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" />
<br /><br />
Which city do you wish to look at hotels for?
<br /><br />
<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>
<br /><br />
<input type="Submit">
</form>
</body>
</html>
Get selected radio button from radio button list (C#)
<%@ 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">
<div>
How did you hear about our Website?
<ul>
<li>
<asp:RadioButton
id="rdlMagazine"
Text="Magazine Article"
GroupName="Source"
Runat="server" />
</li>
<li>
<asp:RadioButton
id="rdlTelevision"
Text="Television Program"
GroupName="Source"
Runat="server" />
</li>
<li>
<asp:RadioButton
id="rdlOther"
Text="Other Source"
GroupName="Source"
Runat="server" />
</li>
</ul>
<asp:Button
id="btnSubmit"
Text="Submit"
Runat="server" OnClick="btnSubmit_Click" />
<hr />
<asp:Label
id="lblResult"
Runat="server" />
</div>
</form>
</body>
</html>
Get selected value from asp:radiobuttonlist (C#)
<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" />
<br /><br />
Which city do you wish to look at hotels for?
<br /><br />
<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>
<br /><br />
<input type="Submit">
</form>
</body>
</html>
On radiobutton list changed (VB.net)
<%@ 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) _
& "<!--" & Chr(13) & "alert(""You selected the color: " _
& rbl1.SelectedItem.Text & """)" & 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">
<BR><BR>
<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>
<BR><BR>
</form>
</BODY>
</HTML>
RadioButtonList Control
<%@ 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;
}
}
}
Select case for asp:radiobuttonlist (VB.net)
<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:
<br><br>
<asp:radiobuttonlist id="destination" runat="server">
<asp:listitem>Barcelona</asp:listitem>
<asp:listitem>Oslo</asp:listitem>
<asp:listitem>Lisbon</asp:listitem>
</asp:radiobuttonlist>
<br><br>
<input type="submit" value="Submit Choice">
<br><br>
<asp:label id="message" runat="server"/>
</form>
</body>
</html>
select case statement for asp:radiobuttonlist (C#)
<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:
<br><br>
<asp:radiobuttonlist id="Destination" runat="server">
<asp:listitem>Barcelona</asp:listitem>
<asp:listitem>Oslo</asp:listitem>
<asp:listitem>Lisbon</asp:listitem>
</asp:radiobuttonlist>
<br><br>
<input type="submit" value="Submit Choice">
<br><br>
<asp:label id="Message" runat="server"/>
</form>
</body>
</html>
Set asp:RadioButtonList Repeat Layout (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>
Setting and Retrieving the Selected Item in a RadioButton Control Group (VB.net)
<%@ 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"
>
<BR><BR>
<asp:Label
id="lblColorSelected"
runat="server"
/>
<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"
/>
<BR><BR>
<asp:button
id="butOK"
text="OK"
Type="Submit"
OnClick="SubmitBtn_Click"
runat="server"
/>
</form>
</BODY>
</HTML>