CLASSIC ASP PAGINATION - asp-classic

I use this code to show pagination of recordset in classic asp + Mysql:
<ul class="pagination">
<% IF Cint(PageNo) > 1 then %>
<li><a rel="1" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="1">Prime</a></li>
<li><a rel="<%=PageNo-1%>" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="<%=PageNo-1%>"><</a></li>
<% End IF%>
<% For intID=1 To TotalPage%>
<% if intID=Cint(PageNo) Then%>
<li><%=intID%></li>
<%Else%>
<li><a rel="<%=intID%>" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="<%=intID%>"><%=intID%></a></li>
<%End IF%>
<%Next%>
<% IF Cint(PageNo) < TotalPage Then %>
<li><a rel="<%=PageNo+1%>" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="<%=PageNo+1%>">></a></li>
<li><a rel="<%=TotalPage%>" href="#" data-topic="<%=Request.QueryString("TOPIC_ID")%>" data-page="<%=TotalPage%>">Ultime</a></li>
<% End IF%>
</ul>
But If I have a lot of pageresults, it show a long line of number.... How could show only 5 page an when change page, show next?
like so:
first < 1 2 3 4 5 > last
and if I click on 5
first < 5 6 7 8 9 > last
etc...

This code works also if you want to keep some other querystring parameters. It removes the page value, adds the new page value and builds the paging control html.
It uses the Twitter Bootstrap Styles: http://twitter.github.io/bootstrap/components.html#pagination
Usage:
Response.Write PagingControl(10, 30, "?field-keywords=whatever&page=7")
Code:
Public Function RemoveEmptyQueryStringParameters(strQueryString)
If IsNullOrEmpty(strQueryString) Then Exit Function
Dim strNewQueryString: strNewQueryString = ""
strQueryString = Replace(strQueryString, "&", "&")
strQueryString = Replace(strQueryString, "?", "&")
Dim arrQueryString: arrQueryString = Split(strQueryString ,"&")
For i=0 To UBound(arrQueryString)
strTempParameter = Left( arrQueryString(i), Instr( arrQueryString(i) & "=", "=" ) - 1 )
strTempParameterValue = Right( arrQueryString(i), Len( arrQueryString(i) ) - InstrRev( arrQueryString(i), "=" ) )
If Not IsNullOrEmpty(strTempParameterValue) Then
strNewQueryString = strNewQueryString & "&" & arrQueryString(i)
End If
Next
If InStr(strNewQueryString,"&") = 1 Then
strNewQueryString = "?" & Right(strNewQueryString, Len(strNewQueryString) - 1)
End If
strNewQueryString = Replace(strNewQueryString, "&", "&")
Erase arrQueryString
Set arrQueryString = Nothing
RemoveEmptyQueryStringParameters = Trim(strNewQueryString)
End Function
Public Function AddQueryStringParameter(ByVal strQueryString, ByVal strParameter, ByVal strValue)
Dim strNewQueryString: strNewQueryString = ""
strNewQueryString = Replace(strQueryString, "&", "&")
strNewQueryString = Replace(strNewQueryString, "?", "&")
strNewQueryString = strNewQueryString & "&" & strParameter & "=" & strValue
If InStr(strNewQueryString,"&") = 1 Then
strNewQueryString = "?" & Right(strNewQueryString, Len(strNewQueryString) - 1)
End If
strNewQueryString = Replace(strNewQueryString, "&", "&")
AddQueryStringParameter = Trim(strNewQueryString)
End Function
Public Function PagingControl(ByVal intPage, ByVal intPageCount, ByVal strQueryString)
If intPageCount <= 1 Then
PagingControl = ""
Exit Function
End If
strQueryString = RemoveEmptyQueryStringParameters(strQueryString)
strQueryString = RemoveQueryStringParameter(strQueryString, "page")
Dim strQueryStringPaging: strQueryStringPaging = ""
Dim strHtml: strHtml = "<div class=""pagination""><ul>"
If cInt(intPage) > 1 Then
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", "1")
strHtml = strHtml & "<li>Anfang</li>"
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", CInt(intPage - 1))
strHtml = strHtml & "<li>< Zurück</li>"
Else
strHtml = strHtml & "<li class=""disabled"">Anfang</li>" & _
"<li class=""disabled"">< Zurück</li>"
End If
Dim intPagesToShow: intPagesToShow = 10
If intPageCount >= intPagesToShow Then
If Cint(intPage)>Int(intPagesToShow/2) Then
If Cint(intPage)>(intPageCount-Int(intPagesToShow/2)) Then
intStart = intPageCount-intPagesToShow
intEnd = intPageCount
Else
intStart = intPage-Int(intPagesToShow/2)
intEnd = intPage+Int(intPagesToShow/2)
End If
Else
intStart = 1
intEnd = intPagesToShow
End If
Else
intStart=1
intEnd=intPageCount
End If
If intStart=0 Then
intStart=1
End If
For i = intStart To intEnd
If Cint(intPage)=i Then
strHtml = strHtml & "<li class=""active"">" & i & "</li>"
Else
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", Cint(i))
strHtml = strHtml & "<li>" & i & "</li>"
End If
Next
If cInt(intPage) < cInt(intPageCount) Then
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", CInt(intPage + 1))
strHtml = strHtml & "<li>Vorwärts ></li>"
strQueryStringPaging = AddQueryStringParameter(strQueryString, "page", Cint(intPageCount))
strHtml = strHtml & "<li>Ende</li>"
Else
strHtml = strHtml & "<li class=""disabled"">Vorwärts ></li>" & _
"<li class=""disabled end"">Ende</li>"
End If
strHtml = strHtml & "</ul></div>"
PagingControl = Trim(strHtml)
End Function

I needed a simular solution in ASP.
I could not find anything usefull, so I made something myself.
I normally only program in PHP, but that wasn't possible in the case where I made this.
So if my code is a bit slopy, feel free to improve ;)
A little function I cooked up:
<%
page = cInt(Request.QueryString("pg"))
pages = cInt(number of pages)
if page <1 then page = 1
if page > pages then page = pages
Function paginationHTML(page, lastPage, URL)
if page >1 then
paginationHTML = "prev"
paginationHTML = paginationHTML & "1"
end if
if page >2 then
paginationHTML = paginationHTML & "<a>...</a>"
end if
if page >0 then
paginationHTML = paginationHTML & "<a href=""" & URL & page & """ class='jp-current'>"& page &"</a>"
end if
if lastPage >2 then
if page < lastPage-1 then
paginationHTML = paginationHTML & "<a>...</a>"
end if
end if
if page < lastPage then
paginationHTML = paginationHTML & ""&lastPage&""
paginationHTML = paginationHTML & "next"
end if
End Function
%>
use like so:
<%=paginationHTML(page,pages,"?pg=")%>

Related

VBScript runtime error server map path not found

I get the following error when I try to post a request from a form in my ASP application.
Microsoft VBScript runtime error '800a004c'
Path not found
/build-your-own-report/stjreport/includes/includes.inc, line 16
This is my includes.inc file
includes.inc
<%
thisPage = Request.ServerVariables("SCRIPT_NAME")
thisPage = Right(thisPageName,Len(thisPage) - InStrRev(thisPage,"/"))
session.timeout = 5
webDir = "/stjreport"
pdf_path = server.MapPath("/") & webDir & "/pdfs"
tmp_path = server.MapPath("/") & webDir & "/temp"
Function RoundUp(n)
roundUp = Int(n) - CBool(CDbl(n) <> CLng(n))
End Function
Sub cleanItUp(tt)
set incFS = server.createobject("scripting.FileSystemObject")
/*line 16*/ set incFO = incFS.GetFolder(tmp_path)
for each incF in incFO.files
'if instr(1,incF.name,".pdf",1) > 0 then
set tmpF = incFS.GetFile(incF)
if DateDiff("s",tmpF.DateCreated,now) > tt then incFS.deleteFile(tmpF)
'end if
next
set incFO = nothing
set incFS = nothing
End Sub
Function makeRandomString(incLength)
makeRandomString = ""
seedStr = "1,N,O,P,Q,3,W,X,Y,Z,$,4,0,A,B,C,D,E,F,8,G,H,I,J,5,6,7,9,K,L,M,R,2,S,U,V,T,"
rndAry = Split(seedStr,",")
rndName = ""
For inc = 1 to incLength
Randomize
iRandom = Int( UBound( rndAry ) * Rnd )
rndName = rndName & rndAry( iRandom )
next
makeRandomString = rndName
End Function
Sub displayMessage (incMsg,incReturnUrl)
if Len(incMsg) > 0 then
Response.write("<br /><br /><p style='text-align:center; font-family:arial; font_size:larger;'>" & incMsg & "</p>")
%>
<script language="javascript">
alert("<%=Replace(Replace(incMsg,"<br />","\n"),"<br>","\n")%>");
<%
if Len(incReturnUrl) > 0 then
%>
top.location.href = "<%=incReturnUrl%>";
<%
else
%>
top.history.go(-1);
<%
end if
%>
</script>
<%
Response.end()
end if
End Sub
Function isPermittedFile(incFileType,incFile)
incFile = replace(incFile, "\", "/")
if inStr(incFile,"/") > 0 then
incAry = split(incFile,"/")
incFile = incAry(uBound(incFile)-1)
end if
if inStr(incFile,".") = 0 then call (displayMessage("Image file: " & incFile & " sounds not to be a recognized image format",""))
incFileAry = split(incFile,".")
incExt = incFileAry(1)
Select Case LCase(incFileType)
Case "image"
incAllowedStr = ",gif,png,jpg,jpeg,bmp,tiff,"
Case "document"
'incAllowedStr = ",pdf,doc,docx,txt,rtf,doc,docx,xls,xlsx,"
incAllowedStr = ",pdf,"
End Select
isPermittedFile = False
if inStr(1,incAllowedStr,","& LCase(incExt) &",",1) > 0 then isPermittedFile = True
End Function
%>

how to use <a href ></a> in classic asp?

i have three columns column(0),column(1),column(2)and i want tag for these three columns in following code for this line: "strNewContents = strNewContents & "" & columns(0) & "" & columns(1) & "" & columns(2) & "" & vbcrlf:"
actual code:
Dim strFileName1
Dim objFSO, objTextFile
Dim intLineNumber, strNewContents, strReadLineText,strLineNumbers
dim data, columns
strFileName1 = "saveimagename.txt"
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(Server.MapPath(strFileName1))
intLineNumber = 0
strLineNumbers = ""
data = split(objTextFile.readall(), vbcrlf)
for intLineNumber = 0 to ubound(data)
columns = split(data(intLineNumber), ",", 3)
if (ubound(columns) = 2) then
''//strNewContents = "<td class='red'>" & columns(0) & "</td><td class='blue'>" & columns(2) & "</td>"
strNewContents = strNewContents & "<tr><td>" & columns(0) & "</td><td>" & columns(1) & "</td><td>" & columns(2) & "</td></tr>" & vbcrlf
end if
next
You are already using Response.Write with strings.
You simply:
Response.Write "" & linkName & ""

Converting an HTA into an ASP page?

I was given an HTA that I think was created by the HTA_Helpomatic. They want it converted into a classic ASP page. So I made a few basic changes, and I think I almost have it working. But I'm running into a problem - the ole 'VBScript Object Required' bit. It's doing this on the line Set objlst_groupnames = document.getElementById( "list_servicenames" )
Here's the original HTA script - can anyone help me figure out how to properly convert this to ASP? I had a similar page created, but it didn't work the way they wanted (You had to click on each service to toggle it, vs just clicking on a check box). Thanks.
<head>
<title>Start/Stop/Restart Windows Services</title>
<HTA:APPLICATION
APPLICATIONNAME="Start/Stop/Restart Windows Services"
BORDER="thin"
SCROLL="yes"
SINGLEINSTANCE="yes"
ID="oHTA"
>
<APPLICATION:HTA>
</head>
<script language="VBScript">
Sub Window_OnLoad
Set objlst_groupnames = document.getElementById( "list_servicenames" )
If objlst_groupnames Is Nothing Then
MsgBox "A problem was encountered while creating the listview." & vbCRLF & "Please see your administrator."
Else
With objlst_groupnames
.View = 3
.Width = 800
.Height = 600
.SortKey = 0
.Arrange = 0
.LabelEdit = 1
.SortOrder = 0
.Sorted = 1
.MultiSelect = 0
.LabelWrap = -1
.HideSelection = -1
.HideColumnHeaders = 0
.OLEDragMode = 0
.OLEDropMode = 0
.Checkboxes = 1
.FlatScrollBar = 0
.FullRowSelect = 1
.GridLines = 0
.HotTracking = 0
.HoverSelection = 0
.PictureAlignment = 0
.TextBackground = 0
.ForeColor = -2147483640
.BackColor = -2147483643
.BorderStyle = 1
.Appearance = 1
.MousePointer = 0
.Enabled = 1
.ColumnHeaders.Clear
.ColumnHeaders.Add , , "Caption", 150
.ColumnHeaders.Add , , "State", 150
.ColumnHeaders.Add , , "Name", 150
.ColumnHeaders.Add , , "Description", 150
.ColumnHeaders.Add , , "Start Mode", 150
.ListItems.Clear
End With
End If
sComputer = MachineName.Value
ListServices(sComputer)
End Sub
Sub ListServices(sComputer)
ON ERROR RESUME NEXT
sServiceName = ServiceName.Value
Set objList = document.getElementById( "list_servicenames" )
objList.ListItems.Clear
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
if sServiceName = "" then
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
else
Set colItems = objWMIService.ExecQuery("Select * From Win32_Service where DisplayName like '%" & sServiceName & "%'")
end if
For Each objItem in colItems
Set objListItem = objList.ListItems.Add
objListItem.Text = objItem.Caption
objListItem.ListSubItems.Add.Text = objItem.State
objListItem.ListSubItems.Add.Text = objItem.Name
objListItem.ListSubItems.Add.Text = objItem.Description
objListItem.ListSubItems.Add.Text = objItem.StartMode
Next
End Sub
Sub btn_start_onClick()
sComputer = MachineName.Value
for n = 1 to list_servicenames.ListItems.Count
if list_servicenames.ListItems(n).checked = True then
strService = list_servicenames.ListItems(n).ListSubItems(2).Text
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
For each objService in colServiceList
errReturn = objService.StartService()
Next
end if
next
ListServices(sComputer)
End Sub
Sub btn_stop_onClick()
sComputer = MachineName.Value
for n = 1 to list_servicenames.ListItems.Count
if list_servicenames.ListItems(n).checked = True then
strService = list_servicenames.ListItems(n).ListSubItems(2).Text
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
For each objService in colServiceList
errReturn = objService.StopService()
Next
end if
next
ListServices(sComputer)
End Sub
Sub btn_restart_onClick()
sComputer = MachineName.Value
'Stop services
for n = 1 to list_servicenames.ListItems.Count
if list_servicenames.ListItems(n).checked = True then
strService = list_servicenames.ListItems(n).ListSubItems(2).Text
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
For each objService in colServiceList
errReturn = objService.StopService()
Next
end if
next
'Start services
for n = 1 to list_servicenames.ListItems.Count
if list_servicenames.ListItems(n).checked = True then
strService = list_servicenames.ListItems(n).ListSubItems(2).Text
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery ("Select * from Win32_Service where Name='" & strService & "'")
For each objService in colServiceList
errReturn = objService.StartService()
Next
end if
next
ListServices(sComputer)
End Sub
Sub btn_Refresh_onClick()
sComputer = MachineName.Value
ListServices(sComputer)
End Sub
Sub btn_exit_onClick()
Window.Close
End Sub
function list_servicenames_ColumnClick(colheader)
list_servicenames.SortKey = colheader.index-1
end Function
</script>
<body>
Enter Machine Name: <Input Type = "Text" Name = "MachineName">
<Input Type = "Button" Value = "Get Services" Name = "Run_Button" onClick = "Window_OnLoad"> Filter Services by Name: <Input Type = "Text" Name = "ServiceName"><P>
<input type="button" value="Refresh" name="btn_Refresh" id="btn_Refresh" title="Click to refresh the services list">
<input type="button" value="Start" name="btn_start" id="btn_start" title="Click to Start the Services">
<input type="button" value="Stop" name="btn_stop" id="btn_stop" title="Click to Stop the Service">
<input type="button" value="Restart" name="btn_restart" id="btn_restart" title="Click to Restart the Services">
<input type="button" value="Exit" name="btn_exit" id="btn_exit" title="Click to Exit Form ">
<br/>
<OBJECT id="list_servicenames" name="list_servicenames" classid="clsid:BDD1F04B-858B-11D1-B16A-00C0F0283628"></OBJECT>
</body>
To get a client-side click event to activate something on the server you must use something like AJAX.

putting values from database into a drop down list

my tool is in asp. i am using this code for a query in sql
dim req_id
req_id=Request.Form("Req_id")
if req_id<>"" then
Set conn=server.CreateObject("adodb.connection")
conn.Open session("Psrconnect")
Set rs=CreateObject("Adodb.Recordset")
rs.Open "select * from passwords where REQ_ID='"&req_id&"'", conn
i want to put the results of this query into a drop down list. how do i do it? any help is very much appreciated.
Slightly edited code from my working pages :
function HtmlFormOption( byval psReturnValue, byval psDisplayValue ,byval psCurrentDefault)
dim x
if IsNull(psCurrentDefault) then psCurrentDefault = ""
if IsNull(psReturnValue) then psReturnValue = ""
if lCase( cStr(psReturnValue) ) = lCase( cStr(psCurrentDefault)) then
x = "selected "
else
x = ""
end if
HtmlFormOption = "<option " & x & "value='" & psReturnValue & "'>" & psDisplayValue & "</option>"
end function
dim Result, sCode, sWaarde
Result = "<select name='NameCombobox' size='1'>" & vbCrlf
while not objRecLookup.Eof
sCode = objRecLookup.Fields(0) ' first field in result set
sWaarde = objRecLookup.Fields(1) ' second field in result set
if not IsNull(sCode) and not IsNull(sWaarde) then
Result = Result & HtmlFormOption( sCode, sWaarde , psCurrentDft )
end if
objRecLookup.MoveNext
wend
objRecLookup.Close
Result = Result & "</select>" & vbCrlf
And than Response.Write(Result)
Here's a simple solution:
<%
Dim objCommand, objRS
Set objCommand = Server.CreateObject("ADODB.Command")
with objCommand
.ActiveConnection = objConn
.CommandType = adCmdText
.CommandText = "SELECT * FROM PASSWORDS WHERE REQ_ID= '" & req_id & "'"
Set objRS = .Execute
end with
%><select name="selectbox"><%
While NOT objRS.EOF
%><option value="<%=objRS("COLUMN_NAME")%>"><%=objRS("COLUMN_NAME")%></option><%
objRS.MoveNext
Wend
%></select><%
objRS.Close
Set objRS = Nothing
Set objCommand = Nothing
%>

asp pagination problem

Hi i have a problem with this asp pagination. it seems to be putting all the links in the one row, so i think it might have something to do with the check of the int i...
but im not that familar with asp. can anyone shed any light on this problem.
the folders contain pdfs for each day of the month, named A08P2.pdf A09P2.pdf etc...
Thanks
i = 1
Set fc = f.Files
Set ff = f.SubFolders
For Each f1 in fc
intPage = cint(mid(f1.name,2,2))
chrEdition = mid(f1.name,1,1)
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
end if
i = i + 1
Next
You should move the incrementing of the i (i=i+1) inside the if...end if, since if i is 9 and you encounter two chrEditions that are not 'A' then i will become 11 and will never match the closing condition i=10:
if chrEdition = "A" then
if i = 1 then
Response.Write "<tr>"
end if
Response.Write "<td width='40' align='center'><a href=" & sUP & f1.name & " class='blue_11px'>" & intPage & "</a></td>"
if i = 10 then
Response.Write "</tr>"
i = 0
end if
i = i + 1
end if

Resources