Passing Query String in the Source Page
If we want to pass some values to another page it is possible through query string.
Query string is a string which is appended after the URL.
E.g. Response.Redirect( Sample.aspx?name=sam&age=21&sex=female);
In my previous post I have explained that Response.Redirect is used to move from one webpage to another webpage.
Sample.aspx is the destination page to which we want to migrate.
Query string is appended just after the URL, beginning with “?”
?name=sam&age=21&sex=female is the query string in the above example where Sample.aspx is the URL and name, age, sex are the parameters with their values sam, 21,female respectively .
Two parameters are separated by ‘&’ in the above example name and age are two different parameters and they are separated from ‘&’.
?name=sam&age=21
Value to the parameter is assigned after equal to (“=”).
?name=sam
Here name is parameter and sam is value assigned to the parameter.
Retrieving Query string in the Destination Page
Query string is retrieved by a command Request.QueryString(“parameter”) in asp.net
e.g. Request.QueryString["id"]
The first thing we do is to check if the query string exists in the first place before we start using it. It could look like this:
if (Request.QueryString["id"] != null)
{
// Do something with the querystring
}
The only problem with the above check to see if the query string is null, is that we don't take into consideration if the query string is filled or not.
That could lead to unhandled exceptions in the code. Instead we should check for query strings like this:
if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
// Do something with the querystring
}
So we do the check again more thoroughly:
if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"].Length == 5)
{
// Do something with the querystring
}
Now we know that we get a query string suitable for further processing.
You can then do more precise data type checks using the Parse or Convert method of most value types or by some other logic.
search
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment