2011-02-28 67 views
24

我有兩個用於公制和美式測量的單選按鈕。我加載頁面,以便點擊公制單選按鈕。如何設置這兩個按鈕,以便在單擊美國時公制單擊和反之亦然?兩個單選按鈕ASP.NET C#

+3

使用單選按鈕列表http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.radiobuttonlist.aspx – 2011-02-28 17:55:01

+3

單選按鈕列表不允許內任何其他HTML雖然比其他ListItem爲每個單選按鈕。 – 2012-04-11 09:58:41

回答

2

將兩個單選按鈕的GroupName屬性設置爲相同的值。您也可以嘗試使用RadioButtonGroup,它會自動爲您做到這一點。

14

確保其GroupName屬性設置爲同一個名字:

<asp:RadioButton GroupName="MeasurementSystem" runat="server" Text="US" /> 
<asp:RadioButton GroupName="MeasurementSystem" runat="server" Text="Metric" /> 
3
 <asp:RadioButtonList id="RadioButtonList1" runat="server"> 
     <asp:ListItem Selected="True">Metric</asp:ListItem> 
     <asp:ListItem>US</asp:ListItem> 
    </asp:RadioButtonList> 
46

爲了使其工作,你必須兩個單選按鈕的屬性組名設置爲相同的值:

<asp:RadioButton id="rbMetric" runat="server" GroupName="measurementSystem"></asp:RadioButton> 
<asp:RadioButton id="rbUS" runat="server" GroupName="measurementSystem"></asp:RadioButton> 

就個人而言,我更喜歡用一個單選按鈕列表:

<asp:RadioButtonList ID="rblMeasurementSystem" runat="server"> 
    <asp:ListItem Text="Metric" Value="metric" /> 
    <asp:ListItem Text="US" Value="us" /> 
</asp:RadioButtonList> 
6

我可以看到這是一個老問題,如果你想把其他的HTML裏面可以使用與GroupName單選按鈕相同的所有單選按鈕,並在文本屬性設置類似的圖像或您需要的HTML。

<asp:RadioButton GroupName="group1" runat="server" ID="paypalrb" Text="<img src='https://www.paypalobjects.com/webstatic/mktg/logo/bdg_secured_by_pp_2line.png' border='0' alt='Secured by PayPal' style='width: 103px; height: 61px; padding:10px;'>" />