Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET C#4.0
Hello : )
 
When I provide a link with query string to access another page with it, if the target page called using the link without query string I will get an error
 
because the query string requested
 
can I check the query string if exist or not during page load and do an action according to that ?
 
I hope my question is clear Smile | :)
 

 
thank you all Smile | :)
Posted 1-Jul-12 23:24pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

HtmlPage.Document.QueryString will give you a dictionary with a list of all query string values.
If the count is 0, there is no key available.
  Permalink  
v2
Comments
A_K_ at 2-Jul-12 4:40am
   
Nice and effective answer as always. My +5.
Abhinav S at 2-Jul-12 4:53am
   
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
You can try this:
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack){
        if (Request.QueryString["param"] != null)
        {
            //If it exists
            Response.Write(Request.QueryString["param"])
        }
        else{
            //If it is not existing
        }
    }
}
 
All the best.
--Amit
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Yes you can. asuming the query string name is "id", and the type you are expecting is string. you can do this
 

string s = Request.QueryString["id"] as string;
 
if(s != null)
{
   //you have a query string with name ID, do something
}
else
{
   //nope, there is no query string names id here, do something else
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

check the querystring length
if (Request.QueryString.ToString().Length >0)
{
//Request.QueryString is exist
} 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Uri uri = new Uri(Request.RawUrl);
if(uri.Query.Length>0)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi Msaya,
 
Sure you can. It goes something like this:
 
using System;
using System.Web.UI;
 
public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    string v = Request.QueryString["param"];
    if (v != null)
    {
        Response.Write("param is ");
        Response.Write(v);
    }
    }
}
 
Check also this post for more details.
 
http://www.dotnetperls.com/querystring[^]
 
Cheers
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

if (Request.QueryString.ToString().Contains("test"))
{
your code;
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 587
1 Sergey Alexandrovich Kryukov 479
2 Maciej Los 305
3 Mathew Soji 195
4 BillWoodruff 170
0 OriginalGriff 7,356
1 Sergey Alexandrovich Kryukov 6,777
2 DamithSL 5,461
3 Manas Bhardwaj 4,916
4 Maciej Los 4,475


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 24 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100