|
-
Dec 24th, 2004, 09:39 AM
#1
Thread Starter
Frenzied Member
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? 
.
~Peter

-
Dec 25th, 2004, 06:12 PM
#2
PowerPoster
Re: Request.QueryString : Is there a collection?
Dont have .NET here, look into NameValueCollection.
-
Dec 26th, 2004, 02:04 PM
#3
Retired VBF Adm1nistrator
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Dec 26th, 2004, 07:14 PM
#4
Frenzied Member
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.
-
Dec 27th, 2004, 12:22 AM
#5
Thread Starter
Frenzied Member
I tried the .AllKeys collection of Params, but it only returns the key, not the value. How do i get both the key and value?
This is what i did:
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim arKeys() As String = Request.Params.AllKeys
Dim sDisplay As String = ""
For iNum As Integer = 0 To UBound(arKeys)
sDisplay += arKeys(iNum) & "<br>"
Next iNum
Label1.Text = sDisplay
End Sub
When i accessed the page using qsTest.aspx?id=1&name=peter i only got id and name to appear. I want a collection that contains both the key and it's value.
Am i making sense?
~Peter

-
Dec 27th, 2004, 09:36 AM
#6
Frenzied Member
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
Last edited by TomGibbons; Dec 27th, 2004 at 09:43 AM.
Reason: Okay I've changed it to fit your example there
-
Dec 29th, 2004, 12:15 AM
#7
Thread Starter
Frenzied Member
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)
.
~Peter

-
Dec 29th, 2004, 11:33 AM
#8
Frenzied Member
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
-
Dec 29th, 2004, 03:49 PM
#9
Thread Starter
Frenzied Member
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.
~Peter

-
Dec 31st, 2004, 03:38 AM
#10
Hyperactive Member
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
-
Dec 31st, 2004, 08:32 AM
#11
Frenzied Member
Re: Request.QueryString : Is there a collection?
Ooo I take it all back. Good work, pvb
-
Jan 1st, 2005, 10:58 PM
#12
Thread Starter
Frenzied Member
Re: Request.QueryString : Is there a collection?
Wow, that works really great pvb. Thanks a lot!
~Peter

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|