question about URL + GET

J

Jeff

Hey

asp.net 2.0 (C#)

Retriving value:
In my webpage, I need to retrieve a value from the URL, this is a parameter
stored in the url, like this: www.helloworld.com?testid=34
what C# code shall I use to retrive this testid value?

Setting value:
Another thing I'm wondering about is what C# code shall I use to set the
testid value to the URL, lets say I want to add the testid parameter to the
URL, what C# code shall I use to do that?

Jeff
 
M

Mark Fitzpatrick

This is a QueryString variable so you will access it through the QueryString
collection

You'll need to test for a null value to start with before assigning it

if(Request.QueryString["testid"] != null)
something =
Convert.ToInt32(Request.QueryString["testid"].ToString());

The value of the testid is probably a number, so you'll need to convert
it from a string into something else. I tend to add the ToString() onto a
lot of collection operations simply to be sure. You never know when you'll
have a collection that will be a collection of objects instead of strings
and will throw an error. Also, this code doesn't test to ensure that it's a
valid integer varable before it tries the convert function.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
 
J

Jeff

thanks, just another thing I'm wondering about:
How can I set a value in the URL, lets say I want to set the testid in this
URL: www.helloworld.com?testid=34 to another value?

I tryed using Request.QueryString.Set, but get an error saying the
collection is readonly... maybe it can't be done?.. Maybe I instead should
use the session object. I'm trying to send value from 1 user control to a
group of other user control (how many isn't known before runtime)


Mark Fitzpatrick said:
This is a QueryString variable so you will access it through the
QueryString collection

You'll need to test for a null value to start with before assigning it

if(Request.QueryString["testid"] != null)
something =
Convert.ToInt32(Request.QueryString["testid"].ToString());

The value of the testid is probably a number, so you'll need to convert
it from a string into something else. I tend to add the ToString() onto a
lot of collection operations simply to be sure. You never know when you'll
have a collection that will be a collection of objects instead of strings
and will throw an error. Also, this code doesn't test to ensure that it's
a valid integer varable before it tries the convert function.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage


Jeff said:
Hey

asp.net 2.0 (C#)

Retriving value:
In my webpage, I need to retrieve a value from the URL, this is a
parameter stored in the url, like this: www.helloworld.com?testid=34
what C# code shall I use to retrive this testid value?

Setting value:
Another thing I'm wondering about is what C# code shall I use to set the
testid value to the URL, lets say I want to add the testid parameter to
the URL, what C# code shall I use to do that?

Jeff
 
M

Mark Fitzpatrick

Unfortunately it's not possible to alter the querystring variables. You
can't change the variable since you would have to change the hyperlink
itself. The only thing you can do is to create a new link with the new id.

The session object does have a bit of a performance hit so that's something
to keep in mind.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage



Jeff said:
thanks, just another thing I'm wondering about:
How can I set a value in the URL, lets say I want to set the testid in
this URL: www.helloworld.com?testid=34 to another value?

I tryed using Request.QueryString.Set, but get an error saying the
collection is readonly... maybe it can't be done?.. Maybe I instead should
use the session object. I'm trying to send value from 1 user control to a
group of other user control (how many isn't known before runtime)


Mark Fitzpatrick said:
This is a QueryString variable so you will access it through the
QueryString collection

You'll need to test for a null value to start with before assigning it

if(Request.QueryString["testid"] != null)
something =
Convert.ToInt32(Request.QueryString["testid"].ToString());

The value of the testid is probably a number, so you'll need to
convert it from a string into something else. I tend to add the
ToString() onto a lot of collection operations simply to be sure. You
never know when you'll have a collection that will be a collection of
objects instead of strings and will throw an error. Also, this code
doesn't test to ensure that it's a valid integer varable before it tries
the convert function.

Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage


Jeff said:
Hey

asp.net 2.0 (C#)

Retriving value:
In my webpage, I need to retrieve a value from the URL, this is a
parameter stored in the url, like this: www.helloworld.com?testid=34
what C# code shall I use to retrive this testid value?

Setting value:
Another thing I'm wondering about is what C# code shall I use to set the
testid value to the URL, lets say I want to add the testid parameter to
the URL, what C# code shall I use to do that?

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top