views:

1722

answers:

4

I am refactoring some legacy code. The app was not using querystrings. The previous developer was hard coding some variables that the app uses in other places.

Like this using VB.NET so.Cpage = "ContractChange.aspx"

My question is can I programatically set this value and include the current querystring?

I want so.Cpage to be something like "ContractChange.aspx?d=1&b=2"

Can I do this with the request object or something? Note, I don't need the domain.

Thanks, ~ck in San Diego

+2  A: 

Try this:

so.Cpage = "ContractChange.aspx?" & Request.RawUrl.Split("?")(1)
Sani Huttunen
A: 

Not sure about the syntax in VB.NET but in C# you would just need to do

string id = Request.QueryString.Get("d");

Hope this helps.

+1  A: 

In VB.Net you can do it with the following.

Dim id As String = Request.Params("RequestId")

If you want to process this in as an integer, you can do the following:

Dim id As Integer

If Integer.TryParse(Request.Params("RequestId"), id) Then
   DoProcessingStuff()
End If
Dillie-O
A: 

To get the current query string you would simply do something like the following:

Dim query as String = Request.QueryString("d")

This will assign the value of the "d" querystring to the string variable "query". Note that all query string values are strings, so if you're passing numbers around, you'll need to "cast" or convert those string values to numerics (be careful of exceptions when casting, though). For example:

Dim query as String = Request.QueryString("d")
Dim iquery as Integer = CType(query, Integer)

The QueryString property of the Request object is a collection of name/value key pairs. Specifically, it's of type System.Collections.Specialized.NameValueCollection, and you can iterate through each of the name/value pairs as so:

Dim coll As System.Collections.Specialized.NameValueCollection = Request.QueryString
Dim value As String
For Each key As String In coll.AllKeys
   value = coll(key)
Next

Using either of these mechanisms (or something very similar) should enable you to construct a string variable which contains the full url (page and querystrings) that you wish to navigate to.

CraigTP
From the question: "...can I programatically set this value and include the current querystring?". I get this to that he wants to append the CURRENT QueryString, not contruct a new one.
Sani Huttunen