Results 1 to 12 of 12

Thread: Request.QueryString : Is there a collection?

  1. #1

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question 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:
    1. 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


  2. #2
    PowerPoster
    Join Date
    Nov 2001
    Location
    Trying to reach and stay in the cloud
    Posts
    2,089

    Re: Request.QueryString : Is there a collection?

    Dont have .NET here, look into NameValueCollection.

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    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]

  4. #4
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    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.


  5. #5

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    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:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim arKeys() As String = Request.Params.AllKeys
    3.         Dim sDisplay As String = ""
    4.         For iNum As Integer = 0 To UBound(arKeys)
    5.             sDisplay += arKeys(iNum) & "<br>"
    6.         Next iNum
    7.         Label1.Text = sDisplay
    8.     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


  6. #6
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    Re: Request.QueryString : Is there a collection?

    Oh okay so you're using VB.NET as opposed to C#. Try this then:
    VB Code:
    1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.      Dim sDisplay As String = ""
    3.  
    4.      For Each sKey As String In Request.QueryString.Keys
    5.           sDisplay += sKey & "=" & Request.QueryString(sKey) & "<br>"
    6.      Next
    7.  
    8.      Label1.Text = sDisplay
    9. 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

  7. #7

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question 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:
    1. Public Function BuildText(ByVal xCompleteQueryString As Request.QueryString.Keys) As String
    2.         Dim sReturn As String = ""
    3.         For Each sKey As String In xCompleteQueryString
    4.             sReturn += sKey & "=" & xCompleteQueryString(sKey) & "<br>"
    5.         Next sKey
    6.         Return sReturn
    7.     End Function
    (obviously that is just an example. there is no such thing as a Request.QueryString.Keys type)
    .
    ~Peter


  8. #8
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    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:
    1. Public Structure udtQueryStringKey
    2.         Dim sKeyName As String
    3.         Dim sKeyValue As String
    4.     End Structure
    5.  
    6.     Dim QueryStringKeys As udtQueryStringKey()
    7.  
    8.     Private Sub Page_Load(ByVal sender As System.Object, _
    9.                                 ByVal e As System.EventArgs) Handles MyBase.Load
    10.         Dim i As Int32 = 0
    11.         For Each sKey As String In Request.QueryString.Keys
    12.             ReDim Preserve QueryStringKeys(i)
    13.  
    14.             QueryStringKeys(i).sKeyName = sKey
    15.             QueryStringKeys(i).sKeyValue = Request.QueryString(sKey)
    16.             i = i + 1
    17.         Next
    18.  
    19.         lblQueryDisplay.Text = BuildText(QueryStringKeys)
    20.     End Sub
    21.  
    22.     Public Function BuildText(ByVal xCompleteQueryString _
    23.                                             As udtQueryStringKey()) As String
    24.         Dim sReturn As String = ""
    25.         Dim i As Int32 = 0
    26.  
    27.         Try
    28.             For i = 0 To xCompleteQueryString.GetUpperBound(0)
    29.                 sReturn += xCompleteQueryString(i).sKeyName & " = " & _
    30.                     xCompleteQueryString(i).sKeyValue & "<br>"
    31.             Next
    32.         Catch ex As NullReferenceException
    33.             ' There is nothing in the array
    34.         End Try
    35.  
    36.         Return sReturn
    37.     End Function

  9. #9

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Cool 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


  10. #10
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366

    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:
    1. <%@ Import Namespace="System.Collections.Specialized" %>
    2. <script runat="server" language="vb">
    3. Protected Overrides Sub OnLoad( ByVal e As System.EventArgs )
    4.     If Not Page.IsPostBack
    5.         SomeMethod( Request.QueryString )
    6.     End If
    7. End Sub
    8. Protected Sub SomeMethod( ByVal querystring As NameValueCollection )
    9.     For Each key As String In querystring
    10.         Response.Write( key )
    11.         Response.Write( " -> " )
    12.         Response.Write( querystring( key ) )
    13.         Response.Write( "<br />" )
    14.     Next
    15. End Sub
    16. </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

  11. #11
    Frenzied Member TomGibbons's Avatar
    Join Date
    Feb 2002
    Location
    San Diego, CA Previous Location: UK
    Posts
    1,345

    Re: Request.QueryString : Is there a collection?

    Ooo I take it all back. Good work, pvb

  12. #12

    Thread Starter
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up 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
  •  



Click Here to Expand Forum to Full Width