2014-12-05 164 views
0

我需要知道RadioButton和RadioButtonList之間的區別,以及在決定使用哪個準則時使用的準則是什麼?RadioButon和RadioButtonList有什麼不同?

我研究這一點,並決定在這裏發表我的發現幫助說明我發現應該有助於澄清我的問題的差異:

我學會了什麼:

單選

使用一次顯示一個單獨的RadioButton。可能需要設置組屬性以將多個RadioButton控件關聯到一個組中。

RadioButtonList的

用於顯示一組單選按鈕控件,自動地提供該組屬性相關聯的所有包括單選按鈕控制成一個單一的基。

觀察

在視覺上,都產生相同的結果在用戶界面中,所提供的一個地方至少2個或更多單選按鈕與該組屬性的值相同的頁面上控制。

UI示例代碼如下

ASP:單選

<asp:RadioButton ID="b2b" text="B to B" checked="true" runat ="server" GroupName="businesstype" /> 
<asp:RadioButton ID="b2c" text="B to C" runat ="server" GroupName="businesstype" /> 

ASP:RadioButtonList的

<asp:RadioButtonList ID="businesstype" runat="server" > 
    <asp:ListItem Selected="True" Value="0">B to B</asp:ListItem> 
    <asp:ListItem Value="1">B to C</asp:ListItem> 
</asp:RadioButtonList> 

什麼是使用各的指引?

+0

想知道反對的原因(除了問題中的措辭外)?這對我很有用,並且從對這個問題的確切標題的良好搜索中出現。如果對問題措辭/文辭進行倒票 - 那爲什麼不編輯問題而不是倒票呢?我今天下班後需要一些時間來編輯這個問題。 – condiosluzverde 2017-11-15 21:01:49

回答

0

您可以在RadioButtonList中獲取選定的索引,因爲它處理ListItem的集合。

您可以visit here for more detail

相比之下,RadioButtonList控制是一個單一的控制充當單選按鈕列表項的集合的父控件。

它從基地ListControl Class派生的,因此作品很像ListBoxDropDownList,並且CheckBoxList Web服務器控件。因此,使用RadioButtonList控件的許多步驟與其他列表Web server controls的步驟相同。

2

1. RadioButtonList的

RadioButtonList的是一個單一的控制與單選按鈕的列表。 這是從ListControl類派生的。因此,這與其他列表控件(如ListBox,DropDownList和CheckBoxList)類似。 爲了給按鈕提供標題,你可以使用Text屬性。您不能在兩個按鈕之間插入文本。 使用「SelectedIndexChanged」事件,您將獲得選定的按鈕值(「RadioButtonList1.SelectedValue」)。

爲e.g

private void Bind() 
{ 
    RadioButtonList1.DataSource = dsEmployees; 
    RadioButtonList1.DataTextField = "EmployeeName"; 
    RadioButtonList1.DataValueField = "EmployeeID"; 
    RadioButtonList1.DataBind(); 
} 

如果使用HTML

<asp:RadioButtonList ID="RadioButtonList1" runat="server" 
    RepeatDirection="Horizontal" 
    onselectedindexchanged="RadioButtonList1_SelectedIndexChanged"> 
    <asp:ListItem Text="Male" Value="1" ></asp:ListItem> 
    <asp:ListItem Text="Female" Value="2" ></asp:ListItem> 
</asp:RadioButtonList> 

2.單選按鈕

單選按鈕」是一個單一的控制,它是衍生自‘複選框’級。您必須設置GroupName屬性來標識組。此外,事件「CheckedChanged」的事件處理程序將幫助我們完成一些工作。另一件事是你必須爲每個單選按鈕編寫單獨的處理程序。

對於例如爲:

<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender" 
    AutoPostBack="true" oncheckedchanged="RadioButton1_CheckedChanged" Text="Male" /> 
    <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender" 
AutoPostBack="true" oncheckedchanged="RadioButton2_CheckedChanged" Text="Female" /> 
0

一種asp:radiobuttonlist創建一組當選擇一個確保單選按鈕的,其他的被取消選擇,而asp:radiobutton不是組內,因此無法通過單擊其他單選按鈕取消選擇。

相關問題