Search code examples
c#asp.netlistupdatepanelcheckboxlist

Displaying only 10 results from a list and the next 10 with button click


Hopefully someone can help. I have a list of objects that i get from the DB. I would like to on load, only add the first ten in the list to a CheckBoxlist. Then with the Next_click I would like to add the next ten and so on. If the user clicks Prev, I would like to add the previous 10. So essentially it is paging through a CheckBoxlist.

This is how i have tried it and didn't succeed, it doesn't throw any errors. Just doesn't do what i want it to do. I hope its possible.

On page load these are declared:

    QuestionHandler quest = null;
    protected List<QuestionView> questions = null;
    int countP1 = 0;
    int countP2 = 10;  

This is the binddata method:

   CheckBoxList1.Items.Clear();
        questions = quest.GetQuestions();
        List<string> display = new List<string>();
        int c = 0;
        foreach (QuestionView qsD in questions)
        {
            if (countP1.ToString().All(char.IsDigit) && countP2.ToString().All(char.IsDigit))
            {
                if (c >= countP1 && c <= countP2)
                {
                    display.Add(qsD.QuestionID.ToString());
                }                  
                c++;
            }
        }
        questions = null;
        questions = new List<QuestionView>();
        foreach(string s in display)
        {
            QuestionView q = new QuestionView();
            q = quest.GetSelectQ(s);
            questions.Add(q);
        }

Then to add it to the checkboxlist(dont worry about the long string, its a premade table):

     foreach (QuestionView qs in questions)
        {
            ListItem item1 = new ListItem();
            item1.Text = "<table class=\"table\" style=\"border: 3px solid #8AC007;\"><tr><td>Title: </td><td width=\"300px\">" + qs.Title + "</td><td>|</td><td>Marks: </td><td width=\"300px\">" + qs.Mark + "</td><td>|</td><td>Type: </td><td width=\"300px\">" + qs.TypeID + "</td></tr><tr><td>Subject:</td><td>" + qs.SubjectID + "</td><td>|</td><td>Topic: </td><td>" + qs.TopicID + "</td><td>|</td><td>Rating: </td><td></td></tr></table>";
            item1.Value = qs.QuestionID.ToString();
            CheckBoxList1.Items.Add(item1);
        }

The next click:

    protected void btnNext_Click(object sender, EventArgs e)
    {            
        countP1 = countP1 + 10;
        countP2 = countP2 + 10;
        BindData();
    }

The prev click:

     protected void btnPrev_Click(object sender, EventArgs e)
    {
        countP1 = countP1 - 10;
        countP2 = countP2 - 10;
        BindData();
    }

Hopefully someone understands what i mean and can help, thank you in advance, feel free to ask me questions about this if you need to. This is all in an update-panel.

Finally, this is how the checkboxlist looks when displayed: Display


Solution

  • You can use LINQ and the methods Take and Skip

    const int size = 10; // How many questions you want to be returned.
    
    public IEnumerable<QuestionView> GetQuestions(int page) 
    {
        return questions.Skip(size * page).Take(size);
    }
    

    This will look at your QuestionView list, skip over 10 records * the page count, and then take the next 10 elements.

    You may want to add some additional logic to ensure that the next set of elements requested does not exceed the QuestionView list limit.

    From your comments:

    For simplicity, you can put the method inside the class you're working with and can invoke it in the DataBinding method (Where you have the following code):

    CheckBoxList1.Items.Clear();
    questions = quest.GetQuestions();
    var pagedQuestions = GetQuestions(1); // Make use of the new method. 
    

    For best practice, you should separate this out and put it somewhere that is related to the QuestionView. You could also put it as an extension to Question View (Extension method).