我在ASP.NET中创建了一个测验应用程序,我使用ListView显示了问题和答案,并使用了DataPager for Next Previous按钮。我想知道如何保留每个问题答案的代码。
答案 0 :(得分:0)
这是静态数据表绑定。您可以将其与数据库值绑定。 aspx页面如
<form id="form1" runat="server">
<div>
<asp:ListView runat="server" ID="lstQuestions" OnItemDataBound="lstQuestions_ItemDataBound">
<ItemTemplate>
<asp:Label runat="server" ID="lblQuestionNo" Text='<%# "Q. "+ Eval("No") %>'></asp:Label>
<asp:Label runat="server" ID="lblQuestion" Text='<%#Eval("Question") %>'></asp:Label><br />
Select Answer:
<br />
<asp:RadioButtonList ID="rdoAnswers" runat="server" RepeatColumns="2" RepeatDirection="Horizontal"></asp:RadioButtonList>
</ItemTemplate>
</asp:ListView>
</div>
</form>
aspx.cs页面如:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//this portion is dynamic
//You can get questions from your database.
DataTable dt = new DataTable();
dt.Columns.Add("No", typeof(int));
dt.Columns.Add("Question");
for (int i = 1; i < 10; i++)
dt.Rows.Add(i, "Your Question -" + i);
lstQuestions.DataSource = dt;
lstQuestions.DataBind();
}
}
protected void lstQuestions_ItemDataBound(object sender, ListViewItemEventArgs e)
{
//this portion is dynamic
//You can get answers from your database.
RadioButtonList rdo = (RadioButtonList)e.Item.FindControl("rdoAnswers");
DataTable dt = new DataTable();
dt.Columns.Add("No", typeof(int));
dt.Columns.Add("Answers");
for (int i = 1; i < 5; i++)
dt.Rows.Add(i, "Your Answer -" + i);
rdo.DataSource = dt;
rdo.DataMember = "Answers";
rdo.DataValueField = "No";
rdo.DataBind();
}