如何在ASP.NET转发器中获取ASP.NET单选按钮的值 [英] How to get the values of ASP.NET radio button inside ASP.NET repeater

查看:68
本文介绍了如何在ASP.NET转发器中获取ASP.NET单选按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网络应用程序中使用asp.net转发器,因为我有四个单选按钮,并且还将数据从数据库提取到转发器和单选按钮。实际上我的问题是我正在设计在线考试网站,我从数据库中获取问题和选项,但只有在提交表格时才考虑第一个问题,但是当涉及到第二个问题时,它只对第二个问题提出第一个问题,等等。如何解决这个问题...请建议我在哪里做错了。



我尝试了什么:



I am using asp.net repeater in the web application in that i am having four radio buttons and also fetching the data from the database to the repeater and radio buttons as well. Actual my problem is I am designing online examination site for which i am getting questions and options from the database but only thing when submitting the form it is considering first question fine but when comes to second question it is taking only first questions to second question and so on. How to solve this problem... Please suggest me where i am doing wrong.

What I have tried:

<div>
    <asp:Repeater ID="QuestionRepeater" runat="server">
        <HeaderTemplate>
            <table>
                <tr>
                    <td>Online Examination</td>
                </tr>
        </HeaderTemplate>
        <SeparatorTemplate>
                <tr>
                    <td>
                        <br />
                    </td>
                </tr>
        </SeparatorTemplate>
        <ItemTemplate>
                <tr>
                    <td>
                        <%#Eval("QuestionNumber") %>
                        <%#Eval("Question") %>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:RadioButton runat="server" ID="rb1" GroupName="Rb_Choice" OnCheckedChanged="CheckChanged" Text='<%# Bind("ChoiceA") %>'></asp:RadioButton>
                    </td>
                    <td>
                        <asp:RadioButton runat="server" ID="rb2" GroupName="Rb_Choice" OnCheckedChanged="CheckChanged" Text='<%# Bind("ChoiceB") %>'></asp:RadioButton>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:RadioButton runat="server" ID="rb3" GroupName="Rb_Choice" OnCheckedChanged="CheckChanged" Text='<%# Bind("ChoiceC") %>'></asp:RadioButton>
                    </td>
                    <td>
                        <asp:RadioButton runat="server" ID="rb4" GroupName="Rb_Choice" OnCheckedChanged="CheckChanged" Text='<%# Bind("ChoiceD") %>'></asp:RadioButton>
                    </td>
                 </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:Repeater>
</div>





CodeBehind:



CodeBehind:

foreach (RepeaterItem rp in QuestionRepeater.Items)
{
    while (dr.Read())
    {
        if (rp.ItemType == ListItemType.Item || rp.ItemType == ListItemType.AlternatingItem)
        {
            a.QuestionNumber = Convert.ToInt32(dr["QuestionNumber"]);
            var rb1 = rp.FindControl("rb1") as RadioButton;
            var rb2 = rp.FindControl("rb2") as RadioButton;
            var rb3 = rp.FindControl("rb3") as RadioButton;
            var rb4 = rp.FindControl("rb4") as RadioButton;
        }
    }
}

推荐答案

我解决了我的问题...一个简单的错误我要交换foreach循环和while循环。



以前先写入循环,然后循环读取数据库中的数据。



I solved my problem... A simple mistake by me that is to interchange foreach loop and while loop.

Previously foreach loop is written first and then while loop to read the data from the database.

foreach (RepeaterItem rp in QuestionRepeater.Items)
{
    while (dr.Read())
    {
        if (rp.ItemType == ListItemType.Item || rp.ItemType == ListItemType.AlternatingItem)
        {
            a.QuestionNumber = Convert.ToInt32(dr["QuestionNumber"]);
            var rb1 = rp.FindControl("rb1") as RadioButton;
            var rb2 = rp.FindControl("rb2") as RadioButton;
            var rb3 = rp.FindControl("rb3") as RadioButton;
            var rb4 = rp.FindControl("rb4") as RadioButton;
        }
    }
}





对于一个示例我只是在循环顶部和foreach循环下来意味着从数据库中读取然后以正确的方式获取问题和选项





For a sample i just changed the order while loop top and foreach loop down that means reading from the database and then getting questions and options in correct way

while (dr.Read())
{
    foreach (RepeaterItem rp in QuestionRepeater.Items)
    {
        if (rp.ItemType == ListItemType.Item || rp.ItemType == ListItemType.AlternatingItem)
        {
            a.QuestionNumber = Convert.ToInt32(dr["QuestionNumber"]);
            var rb1 = rp.FindControl("rb1") as RadioButton;
            var rb2 = rp.FindControl("rb2") as RadioButton;
            var rb3 = rp.FindControl("rb3") as RadioButton;
            var rb4 = rp.FindControl("rb4") as RadioButton;
        }
    }
}


这篇关于如何在ASP.NET转发器中获取ASP.NET单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆