具有RadioButtonList的Repeater的PagedDataSource

时间:2014-01-07 17:34:52

标签: c# asp.net pagination repeater radiobuttonlist

我正在尝试创建一个包含多页问题的考试。问题是在SQL proc中动态创建的。我的aspx页面中有一个Repeater,带有一个RadioButtonList,我在代码隐藏中动态创建。

<asp:Repeater ID="ExamQuestionsRepeater" OnItemDataBound="RepeaterItemEventHandler" runat="server">
            <ItemTemplate>
                <asp:Label ID="QuestionLabel" runat="server"><b><%# Eval("exm_Question") %></b></asp:Label>
                <asp:RadioButtonList ID="QuestionsList" runat="server" RepeatDirection="Vertical">
                </asp:RadioButtonList>
            </ItemTemplate>
            <SeparatorTemplate>
                <tr>
                    <td><hr class="ExamQuestionsSeparatorTemplate"  /></td>
                </tr>
            </SeparatorTemplate>
        </asp:Repeater>

背后的代码如下:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadQuestions();
        }
    }

private void loadQuestions()
    {
            DataSet dataSet = getData();

            PagedDataSource pagedDS = new PagedDataSource();
            pagedDS.DataSource = dataSet.Tables[0].DefaultView;
            pagedDS.AllowPaging = true; //indicate that data is paged
            pagedDS.PageSize = 10; //number of questions per page

            //Edit: I noticed I was setting the page index after I bound the repeater
            pagedDS.CurrentPageIndex = CurrentPage;

            ExamQuestionsRepeater.DataSource = pagedDS;
            ExamQuestionsRepeater.DataBind();

            //update previous and next buttons depending on what current page is at
            updateButtons();
        }

    }

我在转发器中使用OnItemDataBound事件来绑定我的动态单选按钮列表

protected void RepeaterItemEventHandler(object sender, RepeaterItemEventArgs e)
    {
        int questionA = 2;
        int questionB = 3;

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            RadioButtonList questionsRadioButtonList = (RadioButtonList)e.Item.FindControl("QuestionsList");
            DataRow row = ((DataRowView)e.Item.DataItem).Row;

            if (row.ItemArray[questionA] != null && !String.IsNullOrEmpty(row.ItemArray[questionA].ToString()))
            {
                ListItem item = new ListItem();
                item.Text = row.ItemArray[questionA].ToString();
                item.Value = "A";

                questionsRadioButtonList.Items.Add(item);
            }
            if (row.ItemArray[questionB] != null && !String.IsNullOrEmpty(row.ItemArray[questionB].ToString()))
            {
                ListItem item = new ListItem();
                item.Text = row.ItemArray[questionB].ToString();
                item.Value = "B";
                questionsRadioButtonList.Items.Add(item);
            }
        }
    }

    protected void NextButton_Click(object sender, EventArgs e)
    {
        // Set viewstate variable to the next page
        CurrentPage += 1;

        // Reload control
        loadQuestions();
    }

    protected void PreviousButton_Click(object sender, EventArgs e)
    {
        // Set viewstate variable to the previous page
        CurrentPage -= 1;

        // Reload control
        loadQuestions();
    }

我的问题是,当我转到下一页或点击上一页时,问题列表总是绑定到前10个问题,并且状态也不存储在页面之间。在加载下一个“页面”时,我缺少什么或者我需要做什么才能加载下10个项目?

编辑:我正确地在每个页面上加载了下一个项目。现在的问题是,每次加载列表时,我的视图状态都没有保存单选按钮的值。当我使用RepeaterItemEventHandler时,我是否重写它?我应该检查该方法中的某些内容吗?

1 个答案:

答案 0 :(得分:0)

所以我想我误解了控件的选定值是如何加载的。我最终创建了用户答案的​​列表并将其保存到Session变量中。当我在页面之间加载我的问题时,我会检查该会话列表中是否存在问题/答案对,然后从那里加载值。我的RepeaterItemEvenHandler现在看起来像这样,完美地完成了这个技巧:

protected void RepeaterItemEventHandler(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            RadioButtonList questionsRadioButtonList = (RadioButtonList)e.Item.FindControl("QuestionsList");
            ExamQuestion exam = (ExamQuestion)e.Item.DataItem);

            if (!String.IsNullOrEmpty(exam.exm_AnswerA))
            {
                ListItem item = new ListItem();
                item.Text = exam.exm_AnswerA;
                item.Value = "A";

                questionsRadioButtonList.Items.Add(item);
            }

            //loading the rest of the list items for the radiobuttonlist goes here...

            //here is where I load the answer into the radio button if it was answered before
            if (Session["AnswersList"] == null) throw new Exception("Session variable not set properly: AnswersList");

            var answers = (List<UserAnswer>)(Session["AnswersList"]);

            if (answers.FindIndex(a => a.QuestionID == exam.exm_ID.ToString()) > -1)
            {
                questionsRadioButtonList.SelectedValue = 
                answers.Find(a => a.exm_QuestionID == exam.exm_ID.ToString()).exm_UserAnswer;
            }
        }
    }

如果有人需要这种参考,这是一个易于学习的教程链接,有助于创建我的考试:http://www.asp.net/web-forms/videos/building-20-applications/lesson-12-building-a-quiz-engine-2