上午,
我正在写一个简单的测验应用程序。我从我的MS SQL DB中得到了我的问题和答案。我将如何获得这个并将其用于说明转发器控件,因此我可以显示6个问题及其答案。
或者......你知道更好的方法吗?
var questions = dc.Questions.ToList().OrderBy(q => Guid.NewGuid()).Take(6);
foreach (var q in questions)
{
litQuestion.Text = q.question1;
int qId = q.id;
var ans = dc.Answers.Where(a => a.questionId == qId).ToList();
litAnswer1.Text = ans[0].answer1.ToString();
litAnswer2.Text = ans[1].answer1.ToString();
litAnswer3.Text = ans[2].answer1.ToString();
}
dc是我的datacontext。
提前致谢。
答案 0 :(得分:2)
除了其他答案,这是一个非常简单的方法:
假设您的问题对象包含一系列名为答案的可用答案
<asp:Repeater runat="server" DataSourceID="ods">
<ItemTemplate>
<%# Eval("QuestionText") %>
<br />
<asp:RadioButtonList runat="server" DataSource='<%# Eval("Answers") %>' DataTextField="AnswerText" DataValueField="ID">
</asp:RadioButtonList>
</ItemTemplate>
</asp:Repeater>
产生类似的东西:
ods DataSource:
<asp:ObjectDataSource ID="ods" runat="server"
SelectMethod="GetQuestions"
TypeName="WebApplication1.Questions.QuestionsContext">
</asp:ObjectDataSource>
public IEnumerable<Question> GetQuestions()
{
// return your questions
}
public class Question
{
public Guid ID { get; set; }
public string QuestionText { get; set; }
public IEnumerable<Answer> Answers { get; set; }
public bool MultipleAnswers { get; set; }
public bool IsCorrect { get; set; }
public Question()
{
this.Answers = Enumerable.Empty<Answer>();
}
}
public class Answer
{
public Guid ID { get; set; }
public Question Question { get; set; }
public bool IsCorrect { get; set; }
public string AnswerText { get; set; }
public bool WasSelected { get; set; }
}
答案 1 :(得分:0)
这应该让你开始:
标记:
<asp:repeater ID="repeater" runat="server" OnItemDataBound="repeater_OnItemDataBound">
<ItemTemplate>
<asp:Label id="lblQuestion" runat="server" />
<asp:Label id="lblAnswer" runat="server" />
</ItemTemplate>
</asp:repeater>
代码背后:
public void repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
Label lblQuestion = (Label)e.Item.FindControl("lblQuestion");
Label lblAnswer = (Label)e.Item.FindControl("lblAnswer");
//Your logic for getting the questions and answers per iteration
}
}
答案 2 :(得分:0)
如果您不知道每个问题的答案数,可以使用嵌套转发器
<asp:Repeater runat="server" ID="questionsRep" OnItemDataBound="QuestionsRep_ItemDataBound">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("question1") %>'></asp:Label>
<asp:Label runat="server" Text='<%# Eval("id") %>'></asp:Label>
<asp:Repeater runat="server" ID="answersRep">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("answer1") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
在转发器的数据绑定事件中,绑定答案
protected void QuestionsRep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType ==
ListItemType.AlternatingItem))
{
Question q = (Question)e.Item.DataItem;
var answers = dc.Answers.Where(a => a.questionId == q.id).ToList();
Repeater answersRep = (Repeater)e.Item.FindControl("answersRep");
answersRep.DataSource = answers;
answersRep.DataBind();
}
}
答案 3 :(得分:0)
您可以使用服务器端标签(如果您只是显示问题和答案),而不是在转发器的DataBound事件中绑定:
ASPX:
<asp:Repeater ID="quizRepeater" runat="server">
<ItemTemplate>
<div class="fullWidthDiv">
<span class="rightAlignedForQuestion">
<asp:Label ID="questionNumberLabel" runat="server">
Question
<%# DataBinder.Eval(Container.DataItem,"QuestionNumber") %>:
</asp:Label>
</span><span class="leftAlignedForQuestion">
<label>
<%# DataBinder.Eval(Container.DataItem,"Question") %>
</label>
</span>
</div>
<div class="fullWidthDiv">
<span class="leftAlignedForOptions">
Correct Answer:
<asp:Label CssClass="boldFont" id="correctAnswerLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Answer") %>'>
</asp:Label>
</span>
</div>
</ItemTemplate>
</asp:Repeater>
而QuestionNumber,Question和Answers将是绑定列表/ DataTable的属性/列。