aspalliance.com | RemASP Home | domains | authors.aspalliance.com | remas | Library | BuildQueryString

Build Query String

Search

 by Remas Wojciechowski

The following function allows you to build query strings nicely. Simply provide the URL you want to add a query string element to as well as the name and the of the new element. The function returns the URL with the new element. The URL argument can already contain query string elements.

'=============================================================
'= This function adds an element to a URL                    =
'=============================================================
Function AddToQueryString(ByVal strURL, ByVal strKey, ByVal strValue)
    Dim strConChar
    Dim strTemp
    Dim intPos
    intPos = CInt(InStr(strURL, "?"))
    If intPos > 0 Then
        strConChar = "&"
    Else
        strConChar = "?"
    End If
    strTemp = strURL & strConChar & strKey & "=" & Server.URLEncode(strValue)
AddToQueryString = strTemp
End Function

Let's try this function out:

strSampleURL = "http://www.aspalliance.com/remas/page.asp?id=7"
strNewURL = AddToQueryString(strSampleURL, "name", "Good page")
Response.Write(strNewURL)

yields

http://www.aspalliance.com/remas/page.asp?id=7&name=Good+page