如何在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;
}
}
}