当我尝试检查单选按钮是否被选中时,代码不执行

发布于 2024-12-25 02:03:22 字数 2204 浏览 0 评论 0原文

我正在尝试 2 在 ASP.NET 中进行测验。 使用单选按钮显示 mcq 选项。在后面的代码中,当我尝试 2 检查单选按钮是否被选中时,该 if 语句下的 d 代码不执行。 aspx代码:

<ItemTemplate>
            <asp:Literal ID="Literal1" runat="server" Text='<%#Eval("ques") %>'></asp:Literal><br />
            <asp:RadioButton GroupName="a" ID="RadioButton1" Text='<%#Eval("ch1") %>' runat="server" /><br />
            <asp:RadioButton GroupName="a" ID="RadioButton2" Text='<%#Eval("ch2") %>' runat="server" /><br />
            <asp:RadioButton GroupName="a" ID="RadioButton3" Text='<%#Eval("ch3") %>' runat="server" /><br />
            <asp:RadioButton GroupName="a" ID="RadioButton4" Text='<%#Eval("ch4") %>' runat="server" /><br />

            <asp:Label ID="Label1" runat="server" Text='<%#Eval("ans") %>' Visible="false"></asp:Label><br />
            <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
        </ItemTemplate>

代码隐藏:

protected void Button1_Click(object sender, EventArgs e)
    {
        int count = 0;

        foreach(RepeaterItem Items in Repeater1.Items)
        {

            RadioButton r1 = (RadioButton)Items.FindControl("RadioButton1");
            RadioButton r2 = (RadioButton)Items.FindControl("RadioButton2");
            RadioButton r3 = (RadioButton)Items.FindControl("RadioButton3");
            RadioButton r4 = (RadioButton)Items.FindControl("RadioButton4");
            Label l3 = (Label)Items.FindControl("Label3");

            Label l=(Label)Items.FindControl("Label1");
            l3.Text = "hello?";
            if (r1.Checked)
            {
               if(r1.Text==l.Text)
                     count++;
            }
            else
            {
                if (r2.Checked)
                {
                    if(r2.Text==l.Text)
                       count++;
                }
            }
              // and so on for all 4 options
        }
        Label2.Visible = true;
        Label2.Text = "your score is " + count;       //always zero!

    }

i am trying 2 make a quiz in asp.net.
the mcq choices r displayed using radio buttons. in code behind, when i try 2 check if radiobutton is checked, d code under that if statement does not execute.
aspx code:

<ItemTemplate>
            <asp:Literal ID="Literal1" runat="server" Text='<%#Eval("ques") %>'></asp:Literal><br />
            <asp:RadioButton GroupName="a" ID="RadioButton1" Text='<%#Eval("ch1") %>' runat="server" /><br />
            <asp:RadioButton GroupName="a" ID="RadioButton2" Text='<%#Eval("ch2") %>' runat="server" /><br />
            <asp:RadioButton GroupName="a" ID="RadioButton3" Text='<%#Eval("ch3") %>' runat="server" /><br />
            <asp:RadioButton GroupName="a" ID="RadioButton4" Text='<%#Eval("ch4") %>' runat="server" /><br />

            <asp:Label ID="Label1" runat="server" Text='<%#Eval("ans") %>' Visible="false"></asp:Label><br />
            <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label><br />
        </ItemTemplate>

code behind:

protected void Button1_Click(object sender, EventArgs e)
    {
        int count = 0;

        foreach(RepeaterItem Items in Repeater1.Items)
        {

            RadioButton r1 = (RadioButton)Items.FindControl("RadioButton1");
            RadioButton r2 = (RadioButton)Items.FindControl("RadioButton2");
            RadioButton r3 = (RadioButton)Items.FindControl("RadioButton3");
            RadioButton r4 = (RadioButton)Items.FindControl("RadioButton4");
            Label l3 = (Label)Items.FindControl("Label3");

            Label l=(Label)Items.FindControl("Label1");
            l3.Text = "hello?";
            if (r1.Checked)
            {
               if(r1.Text==l.Text)
                     count++;
            }
            else
            {
                if (r2.Checked)
                {
                    if(r2.Text==l.Text)
                       count++;
                }
            }
              // and so on for all 4 options
        }
        Label2.Visible = true;
        Label2.Text = "your score is " + count;       //always zero!

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2025-01-01 02:03:22

如果您正在单步执行调试器,并且您的行

if(r1.Text==l.Text) 
  count++; 

没有执行,那么我猜测行 if (r1.Checked) 的计算结果为 false。

在此页面的 Page_Load() 方法中,是否有这些单选按钮的数据绑定或操作?如果是这样,除非您将它们包装在 if(!Page.IsPostBack){ ... } 条件中,否则它将清除用户对单选按钮所做的任何操作,因此 r1.Checked 将为 false

我希望这可能有所帮助:)祝你好运。

If you're stepping through the debugger, and your line

if(r1.Text==l.Text) 
  count++; 

isn't executing, then I would guess that the line if (r1.Checked) is evaluating to false.

On your Page_Load() method for this page, are there any databindings or manipulation of these radio buttons? If so, unless you wrap them in a if(!Page.IsPostBack){ ... } conditional then it will wipe out whatever the user did to the radio button, hence r1.Checked would be false.

I hope that might help :) Good luck.

煮茶煮酒煮时光 2025-01-01 02:03:22

您需要执行以下操作:

  1. 将单选按钮控件上的 AutoPostBack 设置为 true。
  2. 将转发器控件上的 OnItemCommand 设置为“Button1_Click”
  3. 将 Button1_Click 方法的签名更改为 protected void Button1_Click(object sender, RepeaterCommandEventArgs e)

这至少会触发该方法在你后面的代码中。

You'll need to do the following:

  1. Set the AutoPostBack on the radio button control to true.
  2. Set the OnItemCommand on the repeater control to "Button1_Click"
  3. Change the signature of the Button1_Click method to protected void Button1_Click(object sender, RepeaterCommandEventArgs e)

That will at least trigger the method in your code behind.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文