所有单选按钮均选中c#

所有单选按钮均选中c#,c#,asp.net,C#,Asp.net,在Visual Studio 2012中,我在c#中的3个单选按钮有问题-当我在浏览器中运行此应用程序并选中第一个单选按钮,然后选中第二个单选按钮时,这两个单选按钮都已标记 { public partial class _Default : Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click1(object sender, E

在Visual Studio 2012中,我在c#中的3个单选按钮有问题-当我在浏览器中运行此应用程序并选中第一个单选按钮,然后选中第二个单选按钮时,这两个单选按钮都已标记

{
public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }


    protected void Button1_Click1(object sender, EventArgs e)
    {
        if (RadioButton1.Checked) 
        {
            ListBox1.Items.Add(TextBox1.Text);
            TextBox1.Text = "";             
        }
        else if (RadioButton2.Checked) 
        {
            ListBox2.Items.Add(TextBox1.Text);
            TextBox1.Text = "";
        }
        else if (RadioButton3.Checked) 
        {
            ListBox3.Items.Add(TextBox1.Text);             
            TextBox1.Text = "";
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        {
            ListBox1.Items.Clear();
            ListBox2.Items.Clear();
            ListBox3.Items.Clear();
        }
    }
    }
}







确保将单选按钮的GroupName属性设置为相同的值(该单选按钮组的名称)

例如:


确保将单选按钮的GroupName属性设置为相同的值(该单选按钮组的名称)

例如:



是的。在asp.net中,单选按钮必须位于同一组中,除非使用单选按钮列表控件而不是单选按钮控件。yep。在asp.net中,单选按钮必须位于同一组中,除非使用单选按钮列表控件而不是单选按钮控件。
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <section class="featured">
        <div class="content-wrapper">
            <asp:TextBox ID="TextBox1" runat="server" Width="195px"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Height="32px" Text="Button" OnClick="Button1_Click1" />
            <br />
            <asp:RadioButton ID="RadioButton1" runat="server" />
            <asp:RadioButton ID="RadioButton2" runat="server" />
            <asp:RadioButton ID="RadioButton3" runat="server" />
            <br />
            <br />
            <asp:ListBox ID="ListBox1" runat="server" Height="123px" Width="126px"></asp:ListBox>
            <asp:ListBox ID="ListBox2" runat="server" Height="123px" Width="126px"></asp:ListBox>
            <asp:ListBox ID="ListBox3" runat="server" Height="123px" Width="126px"></asp:ListBox>
            <br />
            <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click"  />
        </div>
    </section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    </asp:Content>
<asp:RadioButton id="Radio1" Text="something" GroupName="RadioGroup1" runat="server" />
<asp:RadioButton id="Radio2" Text="another thing" GroupName="RadioGroup1" runat="server" />
<asp:RadioButton id="Radio3" Text="something else" GroupName="RadioGroup1" runat="server" />