我写了三个repeater的foreach,但是只有第一个执行,剩下的为什么不执行?
不会影响,具体你下断点去调试下。
一、单项选择题(共10题,每题2分) |
---|
'>--%> |
<div style="width:80%;margin-left:auto;margin-right:auto;">
<asp:Repeater ID="Repeater2" runat="server">
<HeaderTemplate><!--头部模板,放表格开始及第一行标题-->
<table class="ts" style="width:100%;"><!--插入表格时只需插入两行即可,显示数据时是根据数据库表循环显示的-->
<tr>
<th style="width:100%;text-align:left;">
二、判断题(共10题,每题2分)</th></tr>
<tr> </HeaderTemplate>
<ItemTemplate><!--项目模板,会进行循环显示,放置表格第二行-->
<tr><td>
<asp:Label ID="Label11" runat="server" Text='<%# Eval("col1") %>'></asp:Label>
<%--<asp:Label ID="Label1" runat="server" Text='<%# Container.ItemIndex + 1% %> '></asp:Label>--%>
<asp:Label ID="Label12" runat="server" Text='<%# Eval("col2") %>'></asp:Label>
<asp:Label ID="Label13" runat="server" Text='<%# Eval("col3") %>' Visible="true"></asp:Label>
<%--<td style="width:100%;text-align:left;">
<%#Eval("col2") %> <!--HTMl中插入其他代码需要用<% %>括起来,Eval("数据库中的字段名")--></td>--%>
</td> </tr>
<tr> <td style="width:100%;text-align:left">
<asp:RadioButton ID="RadioButton11" runat="server" GroupName="1" Text='<%#Eval("col4") %>' /> </td></tr>
<tr> <td style="width:100%;text-align:left;">
<asp:RadioButton ID="RadioButton12" runat="server" GroupName="1" Text='<%#Eval("col5") %>' /></td></tr>
</ItemTemplate>
<FooterTemplate><!--底部模板-->
</table> <!--表格结束部分-->
</FooterTemplate>
</asp:Repeater>
</div>
这是前台的代码,下面的是后台的代码
int score = 0;
//int singlemark = int.Parse(((Label)GridView1.Rows[0].FindControl("Label4")).Text);//取出单选题的每题分值
foreach (RepeaterItem rs in Repeater1.Items)//对单选题每题进行判断用户选择答案
{
string ta = Request.QueryString["tabName"];
string str = "";
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=localhost;Initial Catalog=zh;Integrated Security=True";
// 打开连接
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
if (((RadioButton)rs.FindControl("RadioButton1")).Checked)
{
str = "A";
string aa = ((Label)rs.FindControl("Label1")).Text.Trim();
string mysql1 = "update "+ ta +" set col8='A' where col1="+aa;
SqlCommand cmd1 = new SqlCommand(mysql1, conn);
cmd1.ExecuteNonQuery();
}
else if (((RadioButton)rs.FindControl("RadioButton2")).Checked)
{
str = "B";
string aa = ((Label)rs.FindControl("Label1")).Text.Trim();
string mysql1 = "update " + ta + " set col8='B' where col1=" + aa;
SqlCommand cmd1 = new SqlCommand(mysql1, conn);
cmd1.ExecuteNonQuery();
}
else if (((RadioButton)rs.FindControl("RadioButton3")).Checked)
{
str = "C";
string aa = ((Label)rs.FindControl("Label1")).Text.Trim();
string mysql1 = "update " + ta + " set col8='C' where col1=" + aa;
SqlCommand cmd1 = new SqlCommand(mysql1, conn);
cmd1.ExecuteNonQuery();
}
else if (((RadioButton)rs.FindControl("RadioButton4")).Checked)
{
str = "D";
string aa = ((Label)rs.FindControl("Label1")).Text.Trim();
string mysql1 = "update " + ta + " set col8='D' where col1=" + aa;
SqlCommand cmd1 = new SqlCommand(mysql1, conn);
cmd1.ExecuteNonQuery();
}
if (((Label)rs.FindControl("Label3")).Text.Trim() == str)//将用户选择结果和答案进行比较
{
score = score + 2;
}
}
foreach (RepeaterItem dr in Repeater2.Items)//对判断题每题进行判断用户选择答案
{
string str = "";
if (((RadioButton)dr.FindControl("RadioButton11")).Checked)
{
str = "A";
}
else if (((RadioButton)dr.FindControl("RadioButton12")).Checked)
{
str = "B";
}
if (((Label)dr.FindControl("Label13")).Text.Trim() == str)//将用户选择结果和答案进行比较
{
score = score + 2;
}
Response.Write(score);
}