In ASP.Net, the switches in the same group will switch. Checked status automatically?

In one group on the page there are 2 switches:

<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Group1"/> <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Group1"/> 

In code, I wrote code to test both switches:

 RadioButton1.Checked = true; RadioButton2.Checked = true; 

I thought that RadioButton1.Checked would be false , because they are in the same group, when I check the second, the first will automatically disconnect. But in fact they are both Checked=true .

In my application there is such a switch case:

 // Some code to check the default RadioButton switch(val){ case 1: RadioButton1.Checked = true; case 2: RadioButton2.Checked = true; } 

Therefore, sometimes both Checked switches will be true. This is strange, so I changed the code to:

 switch(val){ case 1: RadioButton1.Checked = true; RadioButton2.Checked = false; case 2: RadioButton1.Checked = false; RadioButton2.Checked = true; } 

This works fine, but what if I need to add 10 more radio buttons, write a long list = true, = false .....?

Any ideas? Thanks!

+4
source share
2 answers

Instead of a switch you will probably be better off:

 RadioButton1.Checked = (val == 1); RadioButton2.Checked = (val == 2); RadioButton3.Checked = (val == 3); // and so on ... RadioButton10.Checked = (val == 10); 

Thus, everything is set to false , with the exception of RadioButton , equal to val . If you had a huge number of RadioButton controls, you might want to place them in an array and pass through it.

+7
source

Assign the groupname property to both radio controls. Radio buttons should have the same property for the group name, unless they behave like flags.

+1
source

Source: https://habr.com/ru/post/1489401/


All Articles