繁体   English   中英

所有单选按钮均已检查c#

[英]All radiobuttons are checked c#

我在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>

确保将单选按钮的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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM