0

我在 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();
        }
    }
    }
}

.

<%@ 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>
4

1 回答 1

5

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

例如:

<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" />
于 2013-11-05T19:26:03.777 回答