Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 23rd, 2007, 06:33 PM
Newbie
 
Join Date: Aug 2007
Posts: 3
Default QueryString Manipulation Using VB.NET

This function will give you 100% control over the QueryString -- perfect for programmers who use QueryString manipulation a lot.

I wrote this function when I kept having to change values of the QueryString and remove some, add some, etc., etc..

It takes four (4) parameters:

oldUrl = the current QueryString you would like manipulated
qsName = the name of the QueryField you would like to modify/remove/add
newValue = the new value you would like for the QueryField. set to "" if removing
del = an optional integer bit flag; if set to 1 it will remove the QueryField/QueryFieldValue from the QueryString.

Here is the code, enjoy! (I am always welcome to new suggestions)

Expand|Select|Wrap|Line Numbers
  1. Function ChangeValue(ByVal oldUrl As String, ByVal qsName As String, ByVal newValue As String, Optional ByVal del As Integer = 0) As String
  2.  
  3.     Dim newUrl As String = ""
  4.  
  5.     ' Check if the [qsName] is currently in the [oldUrl]
  6.     If InStr(oldUrl, qsName & "=")
  7.  
  8.         oldUrl += "&"
  9.  
  10.         Dim pos1 As Integer, pos2 As Integer
  11.  
  12.         If del = 1
  13.             pos1 = oldUrl.IndexOf(qsName & "=")
  14.             pos2 = oldUrl.IndexOf("&", pos1) + 1
  15.         Else
  16.             pos1 = oldUrl.IndexOf(qsName & "=") + qsName.Length + 1
  17.             pos2 = oldUrl.IndexOf("&", pos1)
  18.         End If
  19.  
  20.         Dim chunk_1 As String = oldUrl.SubString(0, pos1)
  21.         Dim chunk_2 As String = oldUrl.SubString(pos2)
  22.  
  23.         If del = 1
  24.             newUrl = chunk_1 & chunk_2
  25.         Else
  26.             newUrl = chunk_1 & newValue & chunk_2
  27.         End If
  28.  
  29.         newUrl = newUrl.SubString(0, (newUrl.Length - 1))
  30.  
  31.     Else
  32.  
  33.         If del = 1
  34.             Return oldUrl
  35.         End If
  36.  
  37.         ' Append the new value to the [oldUrl] and make it a [newUrl]
  38.         If oldUrl.EndsWith("?")
  39.             newUrl = oldUrl & qsName & "=" & newValue
  40.         ElseIf InStr(oldUrl, "?") AndAlso oldUrl.EndsWith("?") = False
  41.             If oldUrl.EndsWith("&")
  42.                 newUrl = oldUrl & qsName & "=" & newValue
  43.             Else
  44.                 newUrl = oldUrl & "&" & qsName & "=" & newValue
  45.             End If
  46.         Else
  47.             newUrl = oldUrl & "?" & qsName & "=" & newValue
  48.         End If
  49.  
  50.     End If
  51.  
  52.     Return newUrl
  53.  
  54. End Function
  55.  
Here is an example:
We have the querystring
Expand|Select|Wrap|Line Numbers
  1. http://www.yourdomain.com/search.aspx?fulltext=cat&author=bill+nye&type=scientist&age=45
Now I want to change the "fulltext" to "dog" and the "author" to "bob dole"
Also, I want to remove "type" from the QueryString.
Expand|Select|Wrap|Line Numbers
  1. Dim newUrl = Request.RawUrl.ToString()
  2. newUrl = ChangeValue(newUrl, "fulltext", "dog")
  3. newUrl = ChangeValue(newUrl, "author", "bob dole")
  4. newUrl = ChangeValue(newUrl, "type", "", 1)
  5.  
The QueryString would now look like this:
Expand|Select|Wrap|Line Numbers
  1. http://www.yourdomain.com/search.aspx?fulltext=dog&author=bob+dole&age=45
Yes! It's that easy!
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles