0

早晨,

我正在编写一个简单的测验应用程序。我从我的 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 是我的数据上下文。

提前致谢。

4

4 回答 4

2

除了其他答案之外,这是一种非常简单的方法:

假设您的Question对象包含一组可用的答案,称为Answers

<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>

这会产生类似的东西:

在此处输入图像描述

编辑 1

ods 数据源:

    <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; }
}
于 2012-07-23T09:38:57.390 回答
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 的属性/列。

于 2012-07-23T09:29:29.383 回答
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
    }
}
于 2012-07-23T09:10:41.537 回答
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>

在转发器的Databound-Event中,绑定答案

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();
    }
}
于 2012-07-23T09:28:15.223 回答