单选按钮不会为标签添加值

时间:2016-03-09 12:48:10

标签: c# asp.net

我尝试添加值并在选择单选按钮后提交到标签。它完全没有这样做。

任何想法?

ASP.NET

<asp:Label ID="CountTest" runat="server" Text="CountTester"></asp:Label>
 <asp:RadioButton ID="RadioButton1" runat="server" />

C#

        int Total = 2 ;

        CountTest.Text = Convert.ToString(Total);


        if (RadioButton1.Checked == true)
        {
            Total = Total + 1;
        }

        else if (radio2.Checked == true)
        {
            Total = 0;
        }

2 个答案:

答案 0 :(得分:0)

请确保您在计数逻辑后指定Total值,如下所示:

    ......
    if (RadioButton1.Checked == true)
    {
        Total = Total + 1;
    }

    else if (radio2.Checked == true)
    {
        Total = 0;
    }

    CountTest.Text = Convert.ToString(Total);   <----- this

另外可以肯定的是,在这个逻辑之后你有一个PostBack。

答案 1 :(得分:0)

您可以使用RadioButtonList:

<asp:RadioButtonList ID="RadioButtonList" runat="server" OnSelectedIndexChanged="RadioButtonList_SelectedIndexChanged" AutoPostBack="True">
        <asp:ListItem Text="+1"  Selected="False"></asp:ListItem>
        <asp:ListItem Text="0"  Selected="True"></asp:ListItem>
    </asp:RadioButtonList>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

通知 - &gt; AutoPostBack="True"

和服务器端:

protected void RadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
    {
        int Total = 2;

        if (RadioButtonList.Items[0].Selected)
        {
            Total++;
        }
        if (RadioButtonList.Items[1].Selected)
        {
            Total = 0;
        }

        Label1.Text = Convert.ToString(Total);
    }