C# 单击时取消选择单选按钮列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18853373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:25:24  来源:igfitidea点击:

Unselect radio button list on click

c#asp.net

提问by Danny

I need to unselect radio button in radio button list, i know it is more sensible to use checkbox but the management wants radio button to be unchecked.

我需要取消选择单选按钮列表中的单选按钮,我知道使用复选框更明智,但管理层希望取消选中单选按钮。

回答by Danny

You mean you want it to be possible for your radio button group to have zero values selected? Technically, you can just ensure that no radio in the group has its checked value set. checked=""or just delete the entire attribute.

您的意思是您希望您的单选按钮组可以选择零值?从技术上讲,您可以确保组中没有无线电设置其检查值。checked=""或者只是删除整个属性。

Be aware that this is an invalid state for the radio group. It's like a boolean that is set to neither true nor false. It makes no sense semantically and is in violation of the HTML spec.

请注意,这是无线电组的无效状态。这就像一个布尔值,既不是真也不是假。它在语义上没有意义,并且违反了 HTML 规范。

If you are constrained to a radio group, the only valid option is to include one radio that represents a 'none of the other options' state.

如果您受限于某个无线电组,则唯一有效的选项是包含一个代表“无其他选项”状态的无线电。

回答by Sankalp

RadioButtonList control is a collection of ListItems. To clear one radio button in the control you need to use Index for that particular item. To clear all radio buttons in the control, there is a method "ClearSelection()".

RadioButtonList 控件是 ListItems 的集合。要清除控件中的一个单选按钮,您需要为该特定项目使用索引。要清除控件中的所有单选按钮,有一个方法“ClearSelection()”。

  //To Unselect First Item
   RadioButtonList1.Items[0].Selected = false;

  //To unselect all Items
  RadioButtonList1.ClearSelection();

hope this will resolve your issue.

希望这能解决您的问题。

回答by user3373745

Use this:

用这个:

RadioButton1.Checked = false;

回答by Ken Baker

I had the same problem. In my case, I was using the radio button checkedChanged event to automatically append a medical documentation string snippet into a rich text box control. The snippet would be different for each radio button selected, and the requirement was that only one choice could be allowed. So radio buttons were the best choice instead of check boxes. The problem was that if the user wanted to remove ALL of the text snippets from the textbox, the s/he could just manually select the text from the box and delete it -- but at least one radio button would remain selected.

我有同样的问题。就我而言,我使用单选按钮 checkedChanged 事件自动将医疗文档字符串片段附加到富文本框控件中。所选的每个单选按钮的代码片段都不同,并且要求只能允许一种选择。所以单选按钮是最好的选择,而不是复选框。问题是,如果用户想从文本框中删除所有文本片段,他/她只需手动从框中选择文本并将其删除——但至少有一个单选按钮将保持选中状态。

So, I found that the easiest way to fix this would be to add a button to the form and use its _Click event to set the specified radio button checked status to false.

因此,我发现解决此问题的最简单方法是向表单添加一个按钮并使用其 _Click 事件将指定的单选按钮选中状态设置为 false。

So, My code looked like this...

所以,我的代码看起来像这样......

    // for rad 1
    private void rad_NoDifferent_CheckedChanged(object sender, EventArgs e) {
        if(rad_NoDifferent.Checked) {
            rtb_GSC_Notes.AppendText(sRating_NoDiffMsg);
            } else {
            rtb_GSC_Notes.Text = rtb_GSC_Notes.Text.Replace(sRating_NoDiffMsg, sNoMsg.TrimEnd());
            }
        }


    // for rad 2
    private void rad_VeryDifferent_CheckedChanged(object sender, EventArgs e) {
        if(rad_VeryDifferent.Checked) {
            rtb_GSC_Notes.AppendText(sRating_VeryDiffMsg);
            } else {
            rtb_GSC_Notes.Text = rtb_GSC_Notes.Text.Replace(sRating_VeryDiffMsg, sNoMsg.TrimEnd());
            }
        }


    // for rad 3
    private void rad_Unsure_CheckedChanged(object sender, EventArgs e) {
        if(rad_Unsure.Checked) {
            rtb_GSC_Notes.AppendText(sRating_UnsureMsg);
            } else {
            rtb_GSC_Notes.Text = rtb_GSC_Notes.Text.Replace(sRating_UnsureMsg, sNoMsg.TrimEnd());
            }
        }

    // for button reset
    private void btn_ClearRadioButtons_Click(object sender, EventArgs e) {
        rad_NoDifferent.Checked = false;
        rad_Unsure.Checked = false;
        rad_VeryDifferent.Checked = false;
        }

回答by Vikas

Recently I was facing same issue however I found the solution with Radio button. below are the aspx code for radio button

最近我遇到了同样的问题,但是我找到了带有单选按钮的解决方案。下面是单选按钮的 aspx 代码

<div>
    <asp:RadioButton ID="RadioButton1" GroupName="myg" onClick="clicked(this.id);" runat="server" />
    <asp:RadioButton ID="RadioButton2" GroupName="myg" onClick="clicked(this.id);"  runat="server" />
    <asp:RadioButton ID="RadioButton3" GroupName="myg" onClick="clicked(this.id);"  runat="server" />
    <asp:RadioButton ID="RadioButton4" GroupName="myg" onClick="clicked(this.id);"  runat="server" />
</div>

Just write below java script.

只需在java脚本下面写。

<script type="type/javascript">

var arr = [];

function clicked(radid) {

var ra = document.getElementById(radid);
if (arr.indexOf(radid) < 0) {
    arr.splice(0, arr.length);
    arr.push(radid);
}
else {
    arr.splice(0, arr.length);
    ra.checked = false;

}
}
</script>

Hope this will solve your purpose.

希望这能解决你的目的。

回答by abhy

private void Clear()
    {
        if(RadioButton1.Checked)
            {
                RadioButton1.Checked = false;
            }
     }

回答by Emre ?zdemir

WPF/C#

WPF/C#

if (RadioBTN.IsChecked == true) {
    RadioBTN.IsChecked = false;
}

回答by Mitr Raiputta

myRadioButtonList.SelectedIndex = -1;

Hope this help.

希望这有帮助。