閱讀此文請先查看網頁教學網的:ASP.NET入門教程:Web服務器控件,簡單講述了Web服務器控件的使用方法。
RadioButtonList 控件用於創建單選按鈕組。創建一組單選按鈕。此控件支持綁定到數據源。
RadioButtonList 控件中的每個可選項是通過 ListItem 元素來定義的!
提示:該控件支持數據綁定!
AppendDataBoundItems, AutoPostBack, CausesValidation, DataTextField, DataTextFormatString, DataValueField, Items, runat, SelectedIndex, SelectedItem, SelectedValue, TagKey, Text, ValidationGroup, OnSelectedIndexChanged
ListControl 控件包括列表控件的所有基本功能。繼承自此控件的控件包括:CheckBoxList, DropDownList, ListBox 以及 RadioButtonList 控件。
AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width
AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible
<asp:RadioButtonList
AccessKey="string"
AppendDataBoundItems="True|False"
AutoPostBack="True|False"
BackColor="color name|#dddddd"
BorderColor="color name|#dddddd"
BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
Inset|Outset"
BorderWidth="size"
CausesValidation="True|False"
CellPadding="integer"
CellSpacing="integer"
CssClass="string"
DataMember="string"
DataSource="string"
DataSourceID="string"
DataTextField="string"
DataTextFormatString="string"
DataValueField="string"
Enabled="True|False"
EnableTheming="True|False"
EnableViewState="True|False"
Font-Bold="True|False"
Font-Italic="True|False"
Font-Names="string"
Font-Overline="True|False"
Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
Large|X-Large|XX-Large"
Font-Strikeout="True|False"
Font-Underline="True|False"
ForeColor="color name|#dddddd"
Height="size"
ID="string"
OnDataBinding="DataBinding event handler"
OnDataBound="DataBound event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnSelectedIndexChanged="SelectedIndexChanged event handler"
OnTextChanged="TextChanged event handler"
OnUnload="Unload event handler"
RepeatColumns="integer"
RepeatDirection="Horizontal|Vertical"
RepeatLayout="Table|Flow"
runat="server"
SelectedIndex="integer"
SelectedValue="string"
SkinID="string"
Style="string"
TabIndex="integer"
TextAlign="Left|Right"
ToolTip="string"
ValidationGroup="string"
Visible="True|False"
Width="size"
>
<asp:ListItem
Enabled="True|False"
Selected="True|False"
Text="string"
Value="string"
/>
</asp:RadioButtonList>
備注:RadioButtonList 控件使您能夠創建單項選擇的單選按鈕組,可以通過綁定到數據源動態生成這個組。若要指定要在 RadioButtonList 控件中顯示的項,請針對每項在 RadioButtonList 控件的開始標記和結束標記之間放置一個 ListItem 元素。
注意:還可以使用 RadioButton 控件。對於使用數據綁定創建一組單選按鈕,RadioButtonList 控件更方便,而單個 RadioButton 控件則可以更好地控制布局。
RadioButtonList 控件還支持數據綁定。若要將控件綁定到數據源,請首先創建數據源(如 ArrayList 對象),該數據源包含要顯示在控件中的項。下一步,使用 DataBind 方法將數據源綁定到 RadioButtonList 控件。使用 DataTextField 和 DataValueField 屬性分別指定數據源中哪個字段綁定到控件中每個列表項的 Text 和 Value 屬性。現在,RadioButtonList 控件將顯示數據源中的信息。加此信息網頁教學網(webjx.com)發布目的是為了防止你變懶!webjx.com不主張采集!
若要確定 RadioButtonList 控件中的選定項,請循環訪問 Items 集合並測試該集合中每一項的 Selected 屬性。
可以使用 RepeatLayout 和 RepeatDirection 屬性指定如何呈現列表。如果將 RepeatLayout 設置為 Table(默認設置),將以表格形式呈現列表。如果將它設置為 Flow,則不會使用任何表格結構呈現列表。默認情況下,RepeatDirection 設置為 Vertical。將此屬性設置為 Horizontal 可以水平呈現該列表。
警告:文本在 RadioButtonList 控件中顯示之前並非 HTML 編碼形式。這使得可以在文本中的 HTML 標記中嵌入腳本。如果控件的值是由用戶輸入的,請務必要對輸入值進行驗證以防止出現安全漏。
下面的示例演示如何使用 RadioButtonList 控件向用戶顯示一組互相排斥的選項。
Visual Basic
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
<script language="VB" runat="server">
Sub Button1_Click(Source As Object, e As EventArgs)
If RadioButtonList1.SelectedIndex > - 1 Then
Label1.Text = "You selected: " & RadioButtonList1.SelectedItem.Text
End If
End Sub
Sub chkLayout_CheckedChanged(sender As Object, e As EventArgs)
If chkLayout.Checked = True Then
RadioButtonList1.RepeatLayout = RepeatLayout.Table
Else
RadioButtonList1.RepeatLayout = RepeatLayout.Flow
End If
End Sub
Sub chkDirection_CheckedChanged(sender As Object, e As EventArgs)
If chkDirection.Checked = True Then
RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal
Else
RadioButtonList1.RepeatDirection = RepeatDirection.Vertical
End If
End Sub
</script>
</head>
<body>
<h3>RadioButtonList Example</h3>
<form runat=server>
<asp:RadioButtonList id=RadioButtonList1 runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:RadioButtonList>
<p>
<asp:CheckBox id=chkLayout OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked=true AutoPostBack="true" runat="server" />
<br>
<asp:CheckBox id=chkDirection OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />
<p>
<asp:Button id=Button1 Text="Submit" onclick="Button1_Click" runat="server"/>
<p>
<asp:Label id=Label1 font-name="Verdana" font-size="8pt" runat="server"/>
</form>
</body>
</html>
C#
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
<script language="C#" runat="server">
void Button1_Click(object Source, EventArgs e)
{
if (RadioButtonList1.SelectedIndex > -1)
{
Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
}
}
void chkLayout_CheckedChanged(Object sender, EventArgs e)
{
if (chkLayout.Checked == true)
{
RadioButtonList1.RepeatLayout = RepeatLayout.Table;
}
else
{
RadioButtonList1.RepeatLayout = RepeatLayout.Flow;
}
}
void chkDirection_CheckedChanged(Object sender, EventArgs e)
{
if (chkDirection.Checked == true)
{
RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
}
else
{
RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
}
}
</script>
</head>
<body>
<h3>RadioButtonList Example</h3>
<form runat=server>
<asp:RadioButtonList id=RadioButtonList1 runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:RadioButtonList>
<p>
<asp:CheckBox id=chkLayout OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked=true AutoPostBack="true" runat="server" />
<br>
<asp:CheckBox id=chkDirection OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />
<p>
<asp:Button id=Button1 Text="Submit" onclick="Button1_Click" runat="server"/>
<p>
<asp:Label id=Label1 font-name="Verdana" font-size="8pt" runat="server"/>
</form>
</body>
</html>
JScript
<%@ Page Language="JScript" AutoEventWireup="True" %>
<html>
<head>
<script language="JScript" runat="server">
function Button1_Click(Source : System.Object, e : EventArgs)
{
if (RadioButtonList1.SelectedIndex > -1)
{
Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
}
}
function chkLayout_CheckedChanged(sender : System.Object, e : EventArgs)
{
if (chkLayout.Checked == true)
{
RadioButtonList1.RepeatLayout = RepeatLayout.Table;
}
else
{
RadioButtonList1.RepeatLayout = RepeatLayout.Flow;
}
}
function chkDirection_CheckedChanged(sender : System.Object, e : EventArgs)
{
if (chkDirection.Checked == true)
{
RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
}
else
{
RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
}
}
</script>
</head>
<body>
<h3>RadioButtonList Example</h3>
<form runat=server>
<asp:RadioButtonList id=RadioButtonList1 runat="server">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:RadioButtonList>
<p>
<asp:CheckBox id=chkLayout OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked=true AutoPostBack="true" runat="server" />
<br>
<asp:CheckBox id=chkDirection OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />
<p>
<asp:Button id=Button1 Text="Submit" onclick="Button1_Click" runat="server"/>
<p>
<asp:Label id=Label1 font-name="Verdana" font-size="8pt" runat="server"/>
</form>
</body>
</html>