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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom paging and sessions...

Status
Not open for further replies.

song2siren

Programmer
Jun 4, 2003
103
GB
Hello

I've created a custom paging solution for a datagrid using a stored procedure, but I just need some advice on the best way to go about preserving the results should someone navigate away from the page and come back again.

The search is always initiated using a query string, and if someone clicks on a 'return to search results' button on another page, the last page they viewed should be displayed.

Any help would be much appreciated.

My code behind for the search looks like the following, with searchText coming from the query string:

Public Sub getResults()

Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connString"))
Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter("Pubs_Search_new", myConnection)

With myDataAdapter
.SelectCommand.CommandType = CommandType.StoredProcedure
.SelectCommand.Parameters.Add ( New SqlParameter ( "@Search", SqlDbType.NVarChar, 255 ) ).Value = searchText
.SelectCommand.Parameters.Add ( New SqlParameter ( "@CurrentPage", SqlDbType.Int ) ).Value = _currentPageNumber
.SelectCommand.Parameters.Add ( New SqlParameter ( "@PageSize", SqlDbType.Int ) ).Value = dgResults.PageSize
.SelectCommand.Parameters.Add ( New SqlParameter ( "@TotalRecords", SqlDbType.Int ) ).Direction = ParameterDirection.Output
End With

Dim resultDS As DataSet
resultDS = New DataSet()
myDataAdapter.Fill(resultDS, "Results")
dgResults.DataSource = resultDS.Tables("Results").DefaultView
dgResults.DataBind()

CurrentPage1.Text = _currentPageNumber.ToString()
CurrentPage2.Text = _currentPageNumber.ToString()
SearchConfirm.Text = searchText

Dim _totalPages As Double = 1
If Not Page.IsPostBack Then
Dim _totalRecords As Int32 = CType(myDataAdapter.SelectCommand.Parameters("@TotalRecords").Value, Int32)
_totalPages = _totalRecords / dgResults.PageSize
TotalPages1.Text = ( System.Math.Ceiling ( _totalPages ) ).ToString()
TotalPages2.Text = ( System.Math.Ceiling ( _totalPages ) ).ToString()
If _totalRecords = 0 Then
TotalItems.Text = "Sorry, no publications matched your query."
ResultsPanel.Visible = False
Else
TotalItems.Text = "There are <strong>" & _totalRecords & "</strong> publications in this category"
ResultsPanel.Visible = True
End If
Else
_totalPages = Double.Parse ( TotalPages1.Text )
End If

If _currentPageNumber = 1 Then

PreviousPage1.Enabled = false
PreviousPage2.Enabled = false

If _totalPages > 1 Then
NextPage1.Enabled = true
NextPage2.Enabled = true
Else
NextPage1.Enabled = false
NextPage2.Enabled = false
End If

Else

PreviousPage1.Enabled = true
PreviousPage2.Enabled = true

If _currentPageNumber = _totalPages Then
NextPage1.Enabled = false
NextPage2.Enabled = false
Else
NextPage1.Enabled = true
NextPage2.Enabled = true
End If

End If

End Sub
 
You probably want to use a Session variable in this instance. The only other viable option would probably be to persist the page number physically and look up the stored value when the user returns.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top