我在Visual Studio 2012中使用c#中的3个radiobuttons时遇到问题 - 当我在浏览器中运行此应用程序并检查第一个单选按钮时,然后检查其中两个都被标记。
{
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>
答案 0 :(得分:5)
确保将单选按钮的“组名”属性设置为相同的值(该组单选按钮的名称)。
例如:
<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" />