我正在使用asp.net C#4.0在vs2010中创建考试网站, 我在问题页面中有详细信息视图
<asp:DetailsView ID="questiondetail" runat="server" AutoGenerateRows="False"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
CellPadding="3" DataSourceID="SqlDataSource1" GridLines="Horizontal"
Height="69px" Width="100%">
<AlternatingRowStyle BackColor="#F7F7F7" />
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
<Fields>
<asp:TemplateField HeaderText="Question" SortExpression="question">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("question") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("question") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("question") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="5px" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="A)" SortExpression="a">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("a") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("a") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="ans"
Text='<%# Eval("a") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="B)" SortExpression="b">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("b") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("b") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="ans"
Text='<%# Eval("b") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="C)" SortExpression="c">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("c") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("c") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton3" runat="server" GroupName="ans"
Text='<%# Eval("c") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="D)" SortExpression="d">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("d") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("d") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:RadioButton ID="RadioButton4" runat="server" GroupName="ans"
Text='<%# Eval("d") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:BoundField DataField="correctans" HeaderText="correctans"
SortExpression="correctans" Visible="False" />
</Fields>
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
</asp:DetailsView>
背后的代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
questiondetail.DataBind();
}
}
string userans = "Not selected";
protected void btnnext_Click(object sender, EventArgs e)
{
try
{
questiondetail.DataBind();
DataRowView dr = (DataRowView)questiondetail.DataItem;
// Create Answer object to save values
Answer a = new Answer();
a.QuestionID = questiondetail.PageIndex.ToString();
a.CorrectAnswer = dr.Row["correctans"].ToString();
//get user ans
questiondetail.DefaultMode = DetailsViewMode.Insert;
RadioButton r1 = questiondetail.FindControl("RadioButton1") as RadioButton;
RadioButton r2 = questiondetail.FindControl("RadioButton2") as RadioButton;
RadioButton r3 = questiondetail.FindControl("RadioButton3") as RadioButton;
RadioButton r4 = questiondetail.FindControl("RadioButton4") as RadioButton;
if (r1.Checked)
{
userans = "A";
}
else if (r2.Checked)
{
userans = "B";
}
else if (r3.Checked)
{
userans = "C";
}
else if (r4.Checked)
{
userans = "D";
}
else
{
userans = "Not selected";
}
questiondetail.DefaultMode = DetailsViewMode.ReadOnly;
a.UserAnswer = userans;
ArrayList al = (ArrayList)Session["AnswerList"];
al.Add(a);
Session.Add("AnswerList", al);
}
catch (Exception ex)
{
lblcatch.Text = ex.Message;
}
if (questiondetail.PageIndex == questiondetail.PageCount - 1)
{
Response.Redirect("Result.aspx");
}
else
{
questiondetail.PageIndex++;
}
if (questiondetail.PageIndex == questiondetail.PageCount - 1)
{
btnnext.Text = "Finish";
}
}
问题是在结果页面上显示每个答案都未选中
那么如何在detailview中检查哪个radiobutton
答案 0 :(得分:0)
尝试删除questiondetail.DefaultMode = DetailsViewMode.Insert;
和questiondetail.DefaultMode = DetailsViewMode.ReadOnly;