2015-10-11 38 views
1

希望有人可以提供幫助。我有一個從DB獲得的對象列表。 我想上的負載,只有在列表中添加的前十到CheckBoxList的。然後用Next_click,我想添加下十個,等等。如果用戶點擊Prev,我想添加前10個。所以本質上它是通過CheckBoxlist進行分頁。顯示只有10從列表的結果和未來10與按鈕單擊

這是多麼我已經嘗試過了,沒有成功,它不會引發任何錯誤。只是不做我想做的事情。我希望它有可能。

在頁面加載這些聲明:

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

這是binddata方法:

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); 
     } 

然後將其添加到CheckBoxList的(不要對長字符串,其預製表憂慮):

 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); 
     } 

下點擊:

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

的上一個點擊:

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

希望有人明白我的意思,可以幫助,謝謝你在前進,隨時問我這個問題,如果你需要。 這一切都在更新面板。

最後,這是怎麼顯示在CheckBoxList的樣子: Display

回答

2

您可以使用LINQ和方法TakeSkip

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); 
} 

這將看您的QuestionView名單,跳過10條記錄*頁數,然後取下10個元素。

您可能需要添加一些額外的邏輯,以確保所要求的下一組元素不超過QuestionView列表限制。

從您的評論:

爲了簡單起見,你可以把你方法正在使用,並且可以在數據綁定的方法調用它裏面的類(如果你有下面的代碼):

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

對於最佳做法,您應該將其分開並將其放在與QuestionView相關的某個位置。你也可以把它作爲Question View(擴展方法)的擴展。

+0

我會把這個放在哪裏?問題是QuestionView對象的列表。我以前從來沒有使用過這種語法,所以我只是說,像下一頁的問題= getquestions(1),然後questions = getquestions(2)?或者我理解這個錯誤? – Cleaven

+0

@Cleaven - 這是正確的,我已經更新了我的答案,包括一些更多的細節。 –

+0

謝謝你的幫助,我確信這會起作用:) – Cleaven

0

從達倫後幫,這是最後的代碼,使其工作:

綁定數據的方法來加載的頁面:

CheckBoxList1.Items.Clear(); 
     questions = quest.GetQuestions(); 
     int count = 0; 
     foreach (QuestionView qs in questions) 
     { 
      if(count<10) //stop it from displaying more than 10 on the first page 
      { 
       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); 
      } 
      count++;     
     } 

下點擊:

countP1++; 
     CheckBoxList1.Items.Clear(); 
     questions = quest.GetQuestions(); 
     var pagedQuestions = GetQuestions(countP1); 
     foreach (QuestionView qs in pagedQuestions) 
     { 
      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); 
     } 

上一個點擊:

countP1--; 
     CheckBoxList1.Items.Clear(); 
     questions = quest.GetQuestions(); 
     var pagedQuestions = GetQuestions(countP1); 
     foreach (QuestionView qs in pagedQuestions) 
     { 
      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); 
     } 

當頁面加載時,count變量被初始化爲0。

相關問題