Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pagination Help: How to create multiple pages.

Status
Not open for further replies.

ironhide1975

Programmer
Feb 25, 2003
451
US
We're looking at some custom code for a page that displays search results. The code looks like it was created under an ASP.NET 1.1 structure. The following code creates the Previous/Next buttons and counts the results. Can someone help me create a Function to add the Page numbers in between these Previous/Next button.

Code:
protected void ShowResults(SearchResultItemCollection items, int nTotal, long nTimeUsed)
		{
			int nCount = 0;

			if(items != null)
			{
				nCount = items.Count;
			}

			Total = nTotal;
			//m_lblTotal.Text = string.Format("Found: {0} documents", nTotal);
			m_lblTotal.Text = string.Format("Results <strong>{0}</strong> to <strong>{1}</strong> of <strong>{2}</strong>", Offset + 1, Offset + nCount, Total);

			ShowButtons();

			if(nCount > 0)
			{
				m_dgResults.DataSource = items;
				m_dgResults.DataBind();
			}

			if(nTotal <= 0)
			{
				m_divSearchWithinResults.Visible = false;
				m_tblDrillDown.Visible = false;
				m_dgResults.Visible = false;
				m_lblNoResults.Visible = true;
				m_lblTotal.Visible = false;
			}
			else
			{
				m_divSearchWithinResults.Visible = true;
				m_tblDrillDown.Visible = true;
				m_dgResults.Visible = true;
				m_lblNoResults.Visible = false;
				m_lblTotal.Visible = true;
			}
		}

		protected void ShowButtons()
		{
			if(Total > Hits)
			{
				m_btnNext.Visible = true;
				m_btnNext2.Visible = true;
			}
			
			if((Offset + Hits) >= Total)
			{
				m_btnNext.Visible = false;
				m_btnNext2.Visible = false;
			}
			else
			{
				m_btnNext.Visible = true;
				m_btnNext2.Visible = true;

				int n = Total - (Offset + Hits);
				if(n < Hits)
				{
					m_btnNext.Text = string.Format("next {0} >>", n);
					m_btnNext2.Text = string.Format("next {0} >>", n);
				}
				else
				{
					m_btnNext.Text = string.Format("next {0} >>", Hits);
					m_btnNext2.Text = string.Format("next {0} >>", Hits);
				}
			}


			if(Offset > 0)
			{
				m_btnPrev.Visible = true;
				m_btnPrev2.Visible = true;

				if(Offset > Hits)
				{
					m_btnPrev.Text = string.Format("<< previous {0}", Hits);
					m_btnPrev2.Text = string.Format("<< previous {0}", Hits);
				}
				else
				{
					m_btnPrev.Text = string.Format("<< previous {0}", Offset);
					m_btnPrev2.Text = string.Format("<< previous {0}", Offset);
				}
			}
			else
			{
				m_btnPrev.Visible = false;
				m_btnPrev2.Visible = false;
			}
		}

 
It looks as though your code uses the "items.Count" value to work out how many items there are. If that is correct you can divide that by the number of records per "page" to work out how many pages there are and then add a control for each page number.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top