Request.QueryString : Is there a collection?
We've all used the QueryString to pass a few values into a page, so code like this is normal:
VB Code:
Dim sName As String = Request.QueryString("Name")
But if i have 3 different pages that all work the same, i'd like to collect all the values passed in via the QueryString, and have a public function parse it.
Is there a special "collection" of QueryString values? And if so, how do i use it? :confused:
.
Re: Request.QueryString : Is there a collection?
Dont have .NET here, look into NameValueCollection.:)
Re: Request.QueryString : Is there a collection?
I'm at a machine at present that doesn't have VB.NET installed... but yes there is a collection.
There is a collection of all data passed - both via QueryString, FORM POST and something else aswell.
Its in the Requst Object.... Ah yes, the Params collection.
Yup, Params :)
Re: Request.QueryString : Is there a collection?
Try
Code:
foreach ( string sKey in Request.QueryString.Keys )
{
// Deal with each key here
}
That's C# obviously. Only slightly different for VB.NET.
:)
Re: Request.QueryString : Is there a collection?
Oh okay so you're using VB.NET as opposed to C#. Try this then:
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sDisplay As String = ""
For Each sKey As String In Request.QueryString.Keys
sDisplay += sKey & "=" & Request.QueryString(sKey) & "<br>"
Next
Label1.Text = sDisplay
End Sub
I've got it to just write out the key and its current value. This should give you an idea of what to do :)
Re: Request.QueryString : Is there a collection?
Yes, that code works TomGibbons. Thanks.
But it doesn't help me find a way to pass it into a function. I want to send the collection of query string values and keys into a function that will process it. The reason is that i have 3 pages that all get values passed in, so i'd like to have one function to process the values instead of having the code in the PageLoad of 3 pages.
Something like this:
VB Code:
Public Function BuildText(ByVal xCompleteQueryString As Request.QueryString.Keys) As String
Dim sReturn As String = ""
For Each sKey As String In xCompleteQueryString
sReturn += sKey & "=" & xCompleteQueryString(sKey) & "<br>"
Next sKey
Return sReturn
End Function
(obviously that is just an example. there is no such thing as a Request.QueryString.Keys type)
.
Re: Request.QueryString : Is there a collection?
Oh okay. Well let's see how well a user defined type will work in this situation.
VB Code:
Public Structure udtQueryStringKey
Dim sKeyName As String
Dim sKeyValue As String
End Structure
Dim QueryStringKeys As udtQueryStringKey()
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Int32 = 0
For Each sKey As String In Request.QueryString.Keys
ReDim Preserve QueryStringKeys(i)
QueryStringKeys(i).sKeyName = sKey
QueryStringKeys(i).sKeyValue = Request.QueryString(sKey)
i = i + 1
Next
lblQueryDisplay.Text = BuildText(QueryStringKeys)
End Sub
Public Function BuildText(ByVal xCompleteQueryString _
As udtQueryStringKey()) As String
Dim sReturn As String = ""
Dim i As Int32 = 0
Try
For i = 0 To xCompleteQueryString.GetUpperBound(0)
sReturn += xCompleteQueryString(i).sKeyName & " = " & _
xCompleteQueryString(i).sKeyValue & "<br>"
Next
Catch ex As NullReferenceException
' There is nothing in the array
End Try
Return sReturn
End Function
Re: Request.QueryString : Is there a collection?
I really thought there would be a collection of some sort that contained all the data, and i could just pass the existing collection to the function.
This will work, but it's not what i expected. I guess i've just grown so accustomed to finding smooth ways of handling data in .NET, that i just assumed they'd have a fancy way to do this as well.
Thanks TomGibbons. I'll use this method to handle the query string on the 3 pages. :thumb:
Re: Request.QueryString : Is there a collection?
It's entirely possible that I'm missing something here, but Request.QueryString _is_ a NameValueCollection and it can be passed around as one. Like this:
VB Code:
<%@ Import Namespace="System.Collections.Specialized" %>
<script runat="server" language="vb">
Protected Overrides Sub OnLoad( ByVal e As System.EventArgs )
If Not Page.IsPostBack
SomeMethod( Request.QueryString )
End If
End Sub
Protected Sub SomeMethod( ByVal querystring As NameValueCollection )
For Each key As String In querystring
Response.Write( key )
Response.Write( " -> " )
Response.Write( querystring( key ) )
Response.Write( "<br />" )
Next
End Sub
</script>
so with this request:
Code:
http://localhost/webform1.aspx?firstname=chris&lastname=carter
the page renders this:
Code:
firstname -> chris
lastname -> carter
Scott Hanselman has a good blog post on Request.QueryString
Re: Request.QueryString : Is there a collection?
Ooo I take it all back. Good work, pvb :)
Re: Request.QueryString : Is there a collection?
Wow, that works really great pvb. Thanks a lot! :thumb: