<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="bishiti.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table> <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> <tr> <td>题干</td> <td>选项A</td> <td>选项B</td> <td>选项C</td> <td>选项D</td> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td> <asp:Label ID="Label1" runat="server" style=" display:none"><%#Eval("id") %></asp:Label> <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Text='<%#Eval("name") %>'></asp:TextBox> </td> <td><asp:RadioButton ID="RadioButton1" runat="server" /><%#Eval("IIA") %></td> <td><asp:RadioButton ID="RadioButton1" runat="server" /><%#Eval("IIB") %></td> <td><asp:RadioButton ID="RadioButton1" runat="server" /><%#Eval("IIC") %></td> <td><asp:RadioButton ID="RadioButton1" runat="server" /><%#Eval("IID") %></td> </tr> </ItemTemplate> </asp:Repeater> </table> <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace bishiti { public partial class WebForm1 : System.Web.UI.Page { TestDBDataContext dataContext = new TestDBDataContext(); List<Name> ListDaAn = new List<Name>(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Show(); } } protected void Button1_Click(object sender, EventArgs e) { int number = 0; //如果选择的答案和编号与重数据库里的一样,则number++ //这个判断怎么写 Number n = new Number(); n.num = number; dataContext.Number.InsertOnSubmit(n); dataContext.SubmitChanges(); Response.Write("提交成功"); } public void Show() { List<Info> list = new List<Info>(); bool foag = true; for (int i = 1; i <= 8; i++) { foag = true; string sql = "select top(1) * from Info order by NEWID()"; var qu = dataContext.ExecuteQuery<Info>(sql); Info a = qu.FirstOrDefault<Info>(); foreach (var item in list) { if (a.id == item.id) { i--; foag = false; break; } } if (foag) { list.Add(a); Name name = new Name(); name.Id = a.id; name.Daan = a.daan; ListDaAn.Add(name); } } Repeater1.DataSource = list; Repeater1.DataBind(); } } public class Name { public int Id { get; set; } public string Daan { get; set; } } }
我是随机从数据库中提取7条试题,然后付到文本域里面,文本域后面跟4个选项(单选按钮),然后点击提交按钮就统计答对的条数保存到Number数据库中。现在的状况就是单选居然可以多选,还有就是请大神帮我把提交按钮怎么比较说说。
<td><asp:RadioButton ID="RadioButton1" runat="server" /><%#Eval("IIA") %></td> <td><asp:RadioButton ID="RadioButton1" runat="server" /><%#Eval("IIB") %></td> <td><asp:RadioButton ID="RadioButton1" runat="server" /><%#Eval("IIC") %></td> <td><asp:RadioButton ID="RadioButton1" runat="server" /><%#Eval("IID") %></td>
改成
<td><input type="radio" name="answer_<%#Eval("id")%>" value="A" /><%#Eval("IIA") %></td> <td><input type="radio" name="answer_<%#Eval("id")%>" value="B" /><%#Eval("IIB") %></td>
<td><input type="radio" name="answer_<%#Eval("id")%>" value="C" /><%#Eval("IIC") %></td>
<td><input type="radio" name="answer_<%#Eval("id")%>" value="D" /><%#Eval("IID") %></td>
后台可以用Request.Form["answer_题目的ID"]取得用户提交的答案
就是不知道这个ID怎么比。
asp:RadioButton 是不是要加一个groupname表示只能单选的 4个设置一样的GroupName