簡體   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