Lots of reading, answer might not be that hard...I am currently working on a set of asp pages that allows my company to keep track of vendors that are attending our meetings. The default asp page allows the user to either go directly to a:
1. link that displays all individual vendors names(vendorind.asp)
or
2. link that displays all vendor companies (vendorcomp.asp)
From vendorcomp.asp the user can select a link next to each vendor company name that will send them to the vendorind.asp displaying only thier individual vendor names. This is done by sending a querystring with the vendors company number (ex. vendormeetings/vendors/vendorind.asp?VendorNumber=302).
The page vendorind.asp displays 20 records by Vendor company. Each record contains the following: ID, VendorNumber, FirstName, LastName. Each field can be sorted and also searched on using a basic search (type in what you are looking for, it searches all fields). The problem I am having is that when I pass VendorNumber to Vendorind using the querystring and more than 20 records are returned and the user selects the next link (or a page number link), the original querystring with the VendorNumber is not passed. I am not sure how to set this up (do I set the querystring as a session, are there issues by doing this?). Here is the code:
<% 'no security checking %>
<%
Response.expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.CacheControl = "no-cache"
%>
<!--#include file="db.asp"-->
<%
displayRecs = 20
recRange = 10
%>
<%
' Get table name
tablename = "[vendorind]"
dbwhere = ""
b_search = ""
%>
<%
' Get search criteria for basic search
pSearch = Request.QueryString("psearch"
If pSearch <> "" Then
pSearch = replace(pSearch,"'","''"
pSearch = replace(pSearch,"[","[[]"
b_search = b_search & "[VendorNumber] LIKE '%" & pSearch & "%' OR "
b_search = b_search & "[FirstName] LIKE '%" & pSearch & "%' OR "
b_search = b_search & "[LastName] LIKE '%" & pSearch & "%' OR "
End If
If len(b_search) > 4 Then
b_search = mid(b_search,1,len(b_search)-4)
b_search = "(" & b_search & ""
End If
%>
<%
'Build search criteria
If b_search <> "" Then
dbwhere = dbwhere & b_search 'basic search
End If
'Save search criteria
If dbwhere <> "" Then
Session("tablename" = tablename
Session("dbwhere" = dbwhere
'reset start record counter
startRec = 1
Session("vendorind_REC" = startRec
Else
If tablename = Session("tablename" Then
dbwhere = Session("dbwhere"
Else
'reset search criteria
dbwhere = ""
Session("dbwhere" = dbwhere
End If
End If
'Get clear search cmd
If Request.QueryString("cmd".Count > 0 then
cmd=Request.QueryString("cmd"
If ucase(cmd) = "RESET" Then
'reset search criteria
dbwhere = ""
Session("dbwhere" = dbwhere
End If
End If
%>
<%
' Load Default Order
DefaultOrder = ""
DefaultOrderType = ""
' Check for an Order parameter
OrderBy = ""
If Request.QueryString("order".Count > 0 Then
OrderBy = Request.QueryString("order"
' Check if an ASC/DESC toggle is required
If Session("vendorind_OB" = OrderBy Then
If Session("vendorind_OT" = "ASC" Then
Session("vendorind_OT" = "DESC"
Else
Session("vendorind_OT" = "ASC"
End if
Else
Session("vendorind_OT" = "ASC"
End If
Session("vendorind_OB" = OrderBy
Session("vendorind_REC" = 1
Else
OrderBy = Session("vendorind_OB"
if OrderBy = "" then
OrderBy = DefaultOrder
Session("vendorind_OB" = OrderBy
Session("vendorind_OT" = DefaultOrderType
End If
End If
' Check for a START parameter
If Request.QueryString("start".Count > 0 Then
startRec = Request.QueryString("start"
Session("vendorind_REC" = startRec
Else
startRec = Session("vendorind_REC"
if not isnumeric(startRec) or startRec = "" then
'reset start record counter
startRec = 1
Session("vendorind_REC" = startRec
End If
End If
' Open Connection to the database
set conn = Server.CreateObject("ADODB.Connection"
conn.Open xDb_Conn_Str
'If there is a vendornum in the string
If Request.Querystring("VendorNumber"<> "" then
myVar1=Request.Querystring("VendorNumber"
strsql = "select * from [vendorind] where VendorNumber ="&myVar1&""
If dbwhere <> "" Then
strsql = strsql & " WHERE " & dbwhere
End If
If OrderBy <> "" then
strsql = strsql & " ORDER BY [" & OrderBy & "] "& Session("vendorind_OT"
End if
set rs = Server.CreateObject("ADODB.Recordset"
rs.Open strsql, conn, 1, 2
totalRecs = rs.RecordCount
'If there is no vendornum in the string
else
strsql = "select * from [vendorind]"
If dbwhere <> "" Then
strsql = strsql & " WHERE " & dbwhere
End If
If OrderBy <> "" then
strsql = strsql & " ORDER BY [" & OrderBy & "] "& Session("vendorind_OT"
End if
set rs = Server.CreateObject("ADODB.Recordset"
rs.Open strsql, conn, 1, 2
totalRecs = rs.RecordCount
End If
%>
<!--#include file="header.asp"-->
<p><font size="-1">TABLE : vendorind</font></p>
<form action="vendorindlist.asp">
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td><font size="-1">Quick Search (*)</font></td>
<td>
<input type="Text" name="psearch" size=10>
<input type="Submit" name="Submit" value="GO!">
</td>
<td><a href="vendorindind.asp?cmd=reset"><font size="-1">Show All</font></a></td>
</tr>
</table>
</form>
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td>
<font size="-1"><a href="VendorComp.asp">Back to Company List</font></a>
</td>
</tr>
</table>
<p>
<form name="vendorindlist">
<table border="0" cellspacing="1" cellpadding="5" bgcolor="#CCCCCC">
<tr bgcolor="#0099CC">
<td>
<a href="vendorindlist.asp?order=<%= Server.URLEncode("ID" %>"><font color="#FFFFFF"><font size="-1">ID </font></font></a>
</td>
<td>
<a href="vendorindlist.asp?order=<%= Server.URLEncode("VendorNumber" %>"><font color="#FFFFFF"><font size="-1">Vendor Number (*)</font></font></a>
</td>
<td>
<a href="vendorindlist.asp?order=<%= Server.URLEncode("CustomerName" %>"><font color="#FFFFFF"><font size="-1">First Name (*)</font></font></a>
</td>
<td>
<a href="vendorindlist.asp?order=<%= Server.URLEncode("CustomerName" %>"><font color="#FFFFFF"><font size="-1">Last Name (*)</font></font></a>
</td>
<td> </td>
<td> </td>
</tr>
<%
'Avoid starting record > total records
if clng(startRec) > clng(totalRecs) then
startRec = totalRecs
end if
'Set the last record to display
stopRec = startRec + displayRecs - 1
'Move to first record directly for performance reason
recCount = startRec - 1
if not rs.eof then
rs.movefirst
rs.move startRec - 1
end if
recActual = 0
Do While (NOT rs.EOF) AND (recCount < stopRec)
recCount = recCount + 1
If Clng(recCount) >= Clng(startRec) Then
recActual = recActual + 1 %>
<%
'set row color
bgcolor="#FFFFFF"
%>
<%
' Display alternate color for rows
If recCount mod 2 <> 0 Then
bgcolor="#F5F5F5"
End If
%>
<%
x_ID = rs("ID"
x_VendorNumber = rs("VendorNumber"
x_FirstName = rs("FirstName"
x_LastName = rs("LastName"
%>
<tr bgcolor="<%= bgcolor %>">
<td><font size="-1">
<% response.write ucase(x_ID) %>
</font></td>
<td><font size="-1">
<% response.write x_VendorNumber %>
</font></td>
<td><font size="-1">
<% response.write ucase(x_FirstName) %>
</font></td>
<td><font size="-1">
<% response.write ucase(x_LastName) %>
</font></td>
<%
end if
rs.MoveNext
Loop
%>
</tr>
</table>
</form>
<%
if totalRecs > 0 then
' Find out if there should be Backward or Forward Buttons on the table.
If startRec = 1 Then
isPrev = False
Else
isPrev = True
PrevStart = startRec - displayRecs
If PrevStart < 1 Then PrevStart = 1 %>
<hr size="1">
<strong><a href="vendorindlist.asp?start=<%=PrevStart%>"><font size="-1">[<< Prev]</font></a></strong>
<%
End If
' Display Page numbers
If (isPrev OR (NOT rs.EOF)) Then
If (NOT isPrev) Then Response.Write "<HR SIZE=1>"
x = 1
y = 1
dx1 = ((startRec-1)\(displayRecs*recRange))*displayRecs*recRange+1
dy1 = ((startRec-1)\(displayRecs*recRange))*recRange+1
If (dx1+displayRecs*recRange-1) > totalRecs then
dx2 = (totalRecs\displayRecs)*displayRecs+1
dy2 = (totalRecs\displayRecs)+1
Else
dx2 = dx1+displayRecs*recRange-1
dy2 = dy1+recRange-1
End If
While x <= totalrecs
If x >= dx1 and x <= dx2 Then
If Clng(startRec) = Clng(x) Then %>
<strong><font size="-1"><%=y%></font></strong>
<% Else %>
<strong><a href="vendorindlist.asp?start=<%=x%>"><font size="-1"><%=y%></font></A></strong>
<% End If
x = x + displayRecs
y = y + 1
elseif x >= (dx1-displayRecs*recRange) and x <= (dx2+displayRecs*recRange) then
if x+recRange*displayRecs < totalRecs then %>
<strong><a href="vendorindlist.asp?start=<%=x%>"><font size="-1"><%=y%>-<%=y+recRange-1%></font></a></strong>
<% else
ny=(totalRecs-1)\displayRecs+1
if ny = y then %>
<strong><a href="vendorindlist.asp?start=<%=x%>"><font size="-1"><%=y%></font></a></strong>
<% else %>
<strong><a href="vendorindlist.asp?start=<%=x%>"><font size="-1"><%=y%>-<%=ny%></font></a></strong>
<% end if
end if
x=x+recRange*displayRecs
y=y+recRange
else
x=x+recRange*displayRecs
y=y+recRange
End If
Wend
End If
' Next link
If NOT rs.EOF Then
NextStart = startRec + displayRecs
isMore = True %>
<strong><a href="vendorindlist.asp?start=<%=NextStart%>"><font size="-1">[Next >>]</font></a></strong>
<% Else
isMore = False
End If %>
<hr size="1">
<% If stopRec > recCount Then stopRec = recCount %>
<font size="-1">Records <%= startRec %> to <%= stopRec %> of <%= totalRecs %></font>
<% Else %>
<br><br>
<font size="-1">No records found!</font>
<br><br>
<% End If %><%
' Close recordset and connection
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing %><!--#include file="footer.asp"-->
1. link that displays all individual vendors names(vendorind.asp)
or
2. link that displays all vendor companies (vendorcomp.asp)
From vendorcomp.asp the user can select a link next to each vendor company name that will send them to the vendorind.asp displaying only thier individual vendor names. This is done by sending a querystring with the vendors company number (ex. vendormeetings/vendors/vendorind.asp?VendorNumber=302).
The page vendorind.asp displays 20 records by Vendor company. Each record contains the following: ID, VendorNumber, FirstName, LastName. Each field can be sorted and also searched on using a basic search (type in what you are looking for, it searches all fields). The problem I am having is that when I pass VendorNumber to Vendorind using the querystring and more than 20 records are returned and the user selects the next link (or a page number link), the original querystring with the VendorNumber is not passed. I am not sure how to set this up (do I set the querystring as a session, are there issues by doing this?). Here is the code:
<% 'no security checking %>
<%
Response.expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.CacheControl = "no-cache"
%>
<!--#include file="db.asp"-->
<%
displayRecs = 20
recRange = 10
%>
<%
' Get table name
tablename = "[vendorind]"
dbwhere = ""
b_search = ""
%>
<%
' Get search criteria for basic search
pSearch = Request.QueryString("psearch"
If pSearch <> "" Then
pSearch = replace(pSearch,"'","''"
pSearch = replace(pSearch,"[","[[]"
b_search = b_search & "[VendorNumber] LIKE '%" & pSearch & "%' OR "
b_search = b_search & "[FirstName] LIKE '%" & pSearch & "%' OR "
b_search = b_search & "[LastName] LIKE '%" & pSearch & "%' OR "
End If
If len(b_search) > 4 Then
b_search = mid(b_search,1,len(b_search)-4)
b_search = "(" & b_search & ""
End If
%>
<%
'Build search criteria
If b_search <> "" Then
dbwhere = dbwhere & b_search 'basic search
End If
'Save search criteria
If dbwhere <> "" Then
Session("tablename" = tablename
Session("dbwhere" = dbwhere
'reset start record counter
startRec = 1
Session("vendorind_REC" = startRec
Else
If tablename = Session("tablename" Then
dbwhere = Session("dbwhere"
Else
'reset search criteria
dbwhere = ""
Session("dbwhere" = dbwhere
End If
End If
'Get clear search cmd
If Request.QueryString("cmd".Count > 0 then
cmd=Request.QueryString("cmd"
If ucase(cmd) = "RESET" Then
'reset search criteria
dbwhere = ""
Session("dbwhere" = dbwhere
End If
End If
%>
<%
' Load Default Order
DefaultOrder = ""
DefaultOrderType = ""
' Check for an Order parameter
OrderBy = ""
If Request.QueryString("order".Count > 0 Then
OrderBy = Request.QueryString("order"
' Check if an ASC/DESC toggle is required
If Session("vendorind_OB" = OrderBy Then
If Session("vendorind_OT" = "ASC" Then
Session("vendorind_OT" = "DESC"
Else
Session("vendorind_OT" = "ASC"
End if
Else
Session("vendorind_OT" = "ASC"
End If
Session("vendorind_OB" = OrderBy
Session("vendorind_REC" = 1
Else
OrderBy = Session("vendorind_OB"
if OrderBy = "" then
OrderBy = DefaultOrder
Session("vendorind_OB" = OrderBy
Session("vendorind_OT" = DefaultOrderType
End If
End If
' Check for a START parameter
If Request.QueryString("start".Count > 0 Then
startRec = Request.QueryString("start"
Session("vendorind_REC" = startRec
Else
startRec = Session("vendorind_REC"
if not isnumeric(startRec) or startRec = "" then
'reset start record counter
startRec = 1
Session("vendorind_REC" = startRec
End If
End If
' Open Connection to the database
set conn = Server.CreateObject("ADODB.Connection"
conn.Open xDb_Conn_Str
'If there is a vendornum in the string
If Request.Querystring("VendorNumber"<> "" then
myVar1=Request.Querystring("VendorNumber"
strsql = "select * from [vendorind] where VendorNumber ="&myVar1&""
If dbwhere <> "" Then
strsql = strsql & " WHERE " & dbwhere
End If
If OrderBy <> "" then
strsql = strsql & " ORDER BY [" & OrderBy & "] "& Session("vendorind_OT"
End if
set rs = Server.CreateObject("ADODB.Recordset"
rs.Open strsql, conn, 1, 2
totalRecs = rs.RecordCount
'If there is no vendornum in the string
else
strsql = "select * from [vendorind]"
If dbwhere <> "" Then
strsql = strsql & " WHERE " & dbwhere
End If
If OrderBy <> "" then
strsql = strsql & " ORDER BY [" & OrderBy & "] "& Session("vendorind_OT"
End if
set rs = Server.CreateObject("ADODB.Recordset"
rs.Open strsql, conn, 1, 2
totalRecs = rs.RecordCount
End If
%>
<!--#include file="header.asp"-->
<p><font size="-1">TABLE : vendorind</font></p>
<form action="vendorindlist.asp">
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td><font size="-1">Quick Search (*)</font></td>
<td>
<input type="Text" name="psearch" size=10>
<input type="Submit" name="Submit" value="GO!">
</td>
<td><a href="vendorindind.asp?cmd=reset"><font size="-1">Show All</font></a></td>
</tr>
</table>
</form>
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td>
<font size="-1"><a href="VendorComp.asp">Back to Company List</font></a>
</td>
</tr>
</table>
<p>
<form name="vendorindlist">
<table border="0" cellspacing="1" cellpadding="5" bgcolor="#CCCCCC">
<tr bgcolor="#0099CC">
<td>
<a href="vendorindlist.asp?order=<%= Server.URLEncode("ID" %>"><font color="#FFFFFF"><font size="-1">ID </font></font></a>
</td>
<td>
<a href="vendorindlist.asp?order=<%= Server.URLEncode("VendorNumber" %>"><font color="#FFFFFF"><font size="-1">Vendor Number (*)</font></font></a>
</td>
<td>
<a href="vendorindlist.asp?order=<%= Server.URLEncode("CustomerName" %>"><font color="#FFFFFF"><font size="-1">First Name (*)</font></font></a>
</td>
<td>
<a href="vendorindlist.asp?order=<%= Server.URLEncode("CustomerName" %>"><font color="#FFFFFF"><font size="-1">Last Name (*)</font></font></a>
</td>
<td> </td>
<td> </td>
</tr>
<%
'Avoid starting record > total records
if clng(startRec) > clng(totalRecs) then
startRec = totalRecs
end if
'Set the last record to display
stopRec = startRec + displayRecs - 1
'Move to first record directly for performance reason
recCount = startRec - 1
if not rs.eof then
rs.movefirst
rs.move startRec - 1
end if
recActual = 0
Do While (NOT rs.EOF) AND (recCount < stopRec)
recCount = recCount + 1
If Clng(recCount) >= Clng(startRec) Then
recActual = recActual + 1 %>
<%
'set row color
bgcolor="#FFFFFF"
%>
<%
' Display alternate color for rows
If recCount mod 2 <> 0 Then
bgcolor="#F5F5F5"
End If
%>
<%
x_ID = rs("ID"
x_VendorNumber = rs("VendorNumber"
x_FirstName = rs("FirstName"
x_LastName = rs("LastName"
%>
<tr bgcolor="<%= bgcolor %>">
<td><font size="-1">
<% response.write ucase(x_ID) %>
</font></td>
<td><font size="-1">
<% response.write x_VendorNumber %>
</font></td>
<td><font size="-1">
<% response.write ucase(x_FirstName) %>
</font></td>
<td><font size="-1">
<% response.write ucase(x_LastName) %>
</font></td>
<%
end if
rs.MoveNext
Loop
%>
</tr>
</table>
</form>
<%
if totalRecs > 0 then
' Find out if there should be Backward or Forward Buttons on the table.
If startRec = 1 Then
isPrev = False
Else
isPrev = True
PrevStart = startRec - displayRecs
If PrevStart < 1 Then PrevStart = 1 %>
<hr size="1">
<strong><a href="vendorindlist.asp?start=<%=PrevStart%>"><font size="-1">[<< Prev]</font></a></strong>
<%
End If
' Display Page numbers
If (isPrev OR (NOT rs.EOF)) Then
If (NOT isPrev) Then Response.Write "<HR SIZE=1>"
x = 1
y = 1
dx1 = ((startRec-1)\(displayRecs*recRange))*displayRecs*recRange+1
dy1 = ((startRec-1)\(displayRecs*recRange))*recRange+1
If (dx1+displayRecs*recRange-1) > totalRecs then
dx2 = (totalRecs\displayRecs)*displayRecs+1
dy2 = (totalRecs\displayRecs)+1
Else
dx2 = dx1+displayRecs*recRange-1
dy2 = dy1+recRange-1
End If
While x <= totalrecs
If x >= dx1 and x <= dx2 Then
If Clng(startRec) = Clng(x) Then %>
<strong><font size="-1"><%=y%></font></strong>
<% Else %>
<strong><a href="vendorindlist.asp?start=<%=x%>"><font size="-1"><%=y%></font></A></strong>
<% End If
x = x + displayRecs
y = y + 1
elseif x >= (dx1-displayRecs*recRange) and x <= (dx2+displayRecs*recRange) then
if x+recRange*displayRecs < totalRecs then %>
<strong><a href="vendorindlist.asp?start=<%=x%>"><font size="-1"><%=y%>-<%=y+recRange-1%></font></a></strong>
<% else
ny=(totalRecs-1)\displayRecs+1
if ny = y then %>
<strong><a href="vendorindlist.asp?start=<%=x%>"><font size="-1"><%=y%></font></a></strong>
<% else %>
<strong><a href="vendorindlist.asp?start=<%=x%>"><font size="-1"><%=y%>-<%=ny%></font></a></strong>
<% end if
end if
x=x+recRange*displayRecs
y=y+recRange
else
x=x+recRange*displayRecs
y=y+recRange
End If
Wend
End If
' Next link
If NOT rs.EOF Then
NextStart = startRec + displayRecs
isMore = True %>
<strong><a href="vendorindlist.asp?start=<%=NextStart%>"><font size="-1">[Next >>]</font></a></strong>
<% Else
isMore = False
End If %>
<hr size="1">
<% If stopRec > recCount Then stopRec = recCount %>
<font size="-1">Records <%= startRec %> to <%= stopRec %> of <%= totalRecs %></font>
<% Else %>
<br><br>
<font size="-1">No records found!</font>
<br><br>
<% End If %><%
' Close recordset and connection
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing %><!--#include file="footer.asp"-->