Two Radio Buttons ASP.NET C#

2020-06-30 04:13发布

I have two radio buttons for metric and US measurements. I load the page so the metric radio button is clicked. How do I set the two buttons so when US is clicked metric unclicks and vise versa?

标签: c# asp.net
5条回答
够拽才男人
2楼-- · 2020-06-30 04:36

Set the GroupName property of both radio buttons to the same value. You could also try using a RadioButtonGroup, which does this for you automatically.

查看更多
男人必须洒脱
3楼-- · 2020-06-30 04:51

In order to make it work, you have to set property GroupName of both radio buttons to the same value:

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

Personally, I prefer to use a RadioButtonList:

<asp:RadioButtonList ID="rblMeasurementSystem" runat="server">
    <asp:ListItem Text="Metric" Value="metric" />
    <asp:ListItem Text="US" Value="us" />
</asp:RadioButtonList>
查看更多
ら.Afraid
4楼-- · 2020-06-30 04:52

Make sure their GroupName properties are set to the same name:

<asp:RadioButton GroupName="MeasurementSystem" runat="server" Text="US" />
<asp:RadioButton GroupName="MeasurementSystem" runat="server" Text="Metric" />
查看更多
Lonely孤独者°
5楼-- · 2020-06-30 04:55

I can see it's an old question, if you want to put other HTML inside could use the radiobutton with GroupName propery same in all radiobuttons and in the Text property set something like an image or the html you need.

   <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;'>" />
查看更多
疯言疯语
6楼-- · 2020-06-30 05:03
     <asp:RadioButtonList id="RadioButtonList1" runat="server">
        <asp:ListItem Selected="True">Metric</asp:ListItem>
        <asp:ListItem>US</asp:ListItem>
     </asp:RadioButtonList>
查看更多
登录 后发表回答