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

Searching Files Within Folder - Page/Search Navigation Problem

Status
Not open for further replies.

raphael232

Programmer
Joined
Jun 9, 2006
Messages
51
Location
GB
Hi, i'm trying to create a search page (searching files within a folder) that allows paging. So far i have 2 functions set up:

Code:
Public Function GetFiles(ByVal strFolderPath As String, ByVal strSearchBy As String, ByVal intPage As Integer, ByVal intMaximumRows As Integer) As DataTable
	Dim DataTable As DataTable
	Dim DataRow As DataRow
	Dim strThumbnailsFolderPath As String = strFolderPath & ".thumbnails/"
	Dim strFolder As String = HttpContext.Current.Server.MapPath(strFolderPath)
	Dim DirInfo As New DirectoryInfo(strFolder)
	Dim intIndex As Integer = 0

	Dim intStartRowIndex As Integer = (intPage - 1) * intMaximumRows
	Dim intEndRowIndex As Integer = intStartRowIndex + intMaximumRows - 1

	' Create a DataTable
	DataTable = New DataTable
	DataTable.Columns.Add(New DataColumn("Path", GetType(String)))
	DataTable.Columns.Add(New DataColumn("Name", GetType(String)))

	' Loop through files in folder
	For Each FI As FileInfo In DirInfo.GetFiles("*" & strSearchBy & "*")
		If intIndex >= intStartRowIndex And intIndex <= intEndRowIndex Then
			DataRow = DataTable.NewRow()
			DataRow(0) = strFolderPath.Substring(1, strFolderPath.Length - 1) & FI.Name
			DataRow(1) = FI.Name
			DataTable.Rows.Add(DataRow)
		End If

		intIndex += 1
	Next

	Return DataTable
End Function

Public Function GetFilesCountBySearch(ByVal strFolderPath As String, ByVal strSearchBy As String) As Integer
	Dim strFolder As String = HttpContext.Current.Server.MapPath(strFolderPath)
	Dim DirInfo As New DirectoryInfo(strFolder)

	Return DirInfo.GetFiles("*" & strSearchBy & "*").Length
End Function

Which works fine as long as you are not searching when not on the first page.

On my page i have the following:

Code:
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:DataList ID="ImagesList" RepeatColumns="3" RepeatDirection="Horizontal" Width="100%" runat="server" CellPadding="5" DataSourceID="ObjectDataSource1">
	<ItemTemplate>
		<a href="javascript:setImage('<%#DataBinder.Eval(Container.DataItem, "Path")%>');"><img src="<%#DataBinder.Eval(Container.DataItem, "ThumbnailPath")%>" alt="<%#DataBinder.Eval(Container.DataItem, "Name")%>" class="NoBorder" /></a>
	</ItemTemplate>
</asp:DataList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetFiles" TypeName="FileSystem">
	<SelectParameters>
		<asp:QueryStringParameter DefaultValue="~/MyFiles/Media/" Name="strFolderPath" QueryStringField="Url" Type="String" />
		<asp:ControlParameter ControlID="txtSearch" Name="strSearchBy" PropertyName="Text"
			Type="String" ConvertEmptyStringToNull="false" />
		<asp:QueryStringParameter DefaultValue="1" Name="intPage" QueryStringField="Page"
			Type="Int32" />
		<asp:Parameter DefaultValue="15" Name="intMaximumRows" Type="Int32" />
	</SelectParameters>
</asp:ObjectDataSource>

which calls the GetFiles method of my class passing in the appropriate values.

The two problems i am getting is:

When searching from a page not on the first it does not work, ie say the maximumrows was set to 15 it would look for the results between 15 to 29 when the results are on 0 to 14. As a temporary fix i set the intPage to 1 if strSearch <> "" before calculating the start and end row indexes.

But this brings me onto my second problem. I need the value of txtSearch to be maintained when clicking next and previous. Is there any way of doing this? If so then the above problem wouldn't work because i am resetting the page to 1 if the strSearch is not blank.

Appreciate if someone could help because my brain is abit friend from all this logic.

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top