S
ScriptNFool
Ok... I've mooched off this group for a while now. Time to give some
code back.
Here's a class I wrote to solve the problem of handling query strings.
I use it for an application where the page needs to preserve the
existing QueryString, but add or remove a couple items and then
redirect it back to the same page or other locations.
....A good example of how polymorphism can be useful for us VB'ers who
are less familiar with the concept.
-SF
Here's the class *****
Imports System.Collections.Specialized
Public Class cQueryString
Inherits NameValueCollection
Public Sub New(ByVal QueryString As NameValueCollection)
Dim i As Integer
With QueryString
'--Cycle through Items and Add them back
For i = 0 To .Count - 1
Me.Add(.GetKey(i), .Item(i))
Next
End With
End Sub
Public Overloads Sub Add(ByVal Name As String, ByVal Value As
String, ByVal Overwrite As Boolean)
If Name = "" Then Exit Sub
If Overwrite = True Then
Me.Remove(Name)
Me.Add(Name, Value)
Else
Me.Add(Name, Value)
End If
End Sub
Public Overrides Function ToString() As String
Dim i As Integer
Dim sbOutput As New System.Text.StringBuilder()
If Me.HasKeys = False Then
'--Return Blank String if Empty QueryString
Return ""
End If
sbOutput.Append("?")
With Me
'--Cycle through Items and Add them back
For i = 0 To .Count - 1
sbOutput.Append(.GetKey(i) & "=" & .Item(i))
If i <> .Count - 1 Then
sbOutput.Append("&")
End If
Next
End With
Return sbOutput.ToString
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
And here's how I use it in my code *****
Dim objQueryString As New cQueryString(Request.QueryString)
objQueryString.Add("SomeKey", "SomeValue", True)
objQueryString.Add("AnotherNeededKey", "AnotherValue", True)
objQueryString.Remove("NoLongerNeededKey")
Response.Redirect("http://www.weblocation.com/myweb" &
objQueryString.ToString)
Another way to use it *****
'-- It's just as easy to clear out all the strings, and add the ones
you want.
Dim objQueryString As New cQueryString(Request.QueryString)
objQueryString.Clear
'-- Just as easy to Add together at once,
'-- or intersperse throughout a procedure as stuff gets determined.
objQueryString.Add("FirstItem", "FirstItemValue", True)
objQueryString.Add("SecondItem", "SecondItemValue", True)
objQueryString.Add("ThirdItem", "ThirdItemValue", True)
Response.Redirect("http://www.weblocation.com/myweb" &
objQueryString.ToString)
'-- Hopefully a little more readible then piling stuff at the end
'-- of a redirect.
code back.
Here's a class I wrote to solve the problem of handling query strings.
I use it for an application where the page needs to preserve the
existing QueryString, but add or remove a couple items and then
redirect it back to the same page or other locations.
....A good example of how polymorphism can be useful for us VB'ers who
are less familiar with the concept.
-SF
Here's the class *****
Imports System.Collections.Specialized
Public Class cQueryString
Inherits NameValueCollection
Public Sub New(ByVal QueryString As NameValueCollection)
Dim i As Integer
With QueryString
'--Cycle through Items and Add them back
For i = 0 To .Count - 1
Me.Add(.GetKey(i), .Item(i))
Next
End With
End Sub
Public Overloads Sub Add(ByVal Name As String, ByVal Value As
String, ByVal Overwrite As Boolean)
If Name = "" Then Exit Sub
If Overwrite = True Then
Me.Remove(Name)
Me.Add(Name, Value)
Else
Me.Add(Name, Value)
End If
End Sub
Public Overrides Function ToString() As String
Dim i As Integer
Dim sbOutput As New System.Text.StringBuilder()
If Me.HasKeys = False Then
'--Return Blank String if Empty QueryString
Return ""
End If
sbOutput.Append("?")
With Me
'--Cycle through Items and Add them back
For i = 0 To .Count - 1
sbOutput.Append(.GetKey(i) & "=" & .Item(i))
If i <> .Count - 1 Then
sbOutput.Append("&")
End If
Next
End With
Return sbOutput.ToString
End Function
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
And here's how I use it in my code *****
Dim objQueryString As New cQueryString(Request.QueryString)
objQueryString.Add("SomeKey", "SomeValue", True)
objQueryString.Add("AnotherNeededKey", "AnotherValue", True)
objQueryString.Remove("NoLongerNeededKey")
Response.Redirect("http://www.weblocation.com/myweb" &
objQueryString.ToString)
Another way to use it *****
'-- It's just as easy to clear out all the strings, and add the ones
you want.
Dim objQueryString As New cQueryString(Request.QueryString)
objQueryString.Clear
'-- Just as easy to Add together at once,
'-- or intersperse throughout a procedure as stuff gets determined.
objQueryString.Add("FirstItem", "FirstItemValue", True)
objQueryString.Add("SecondItem", "SecondItemValue", True)
objQueryString.Add("ThirdItem", "ThirdItemValue", True)
Response.Redirect("http://www.weblocation.com/myweb" &
objQueryString.ToString)
'-- Hopefully a little more readible then piling stuff at the end
'-- of a redirect.