Click here to Skip to main content
15,881,668 members
Please Sign up or sign in to vote.
2.85/5 (4 votes)
See more:
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 :)



thank you all :)
Posted

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.
 
Share this answer
 
v2
Comments
_Amy 2-Jul-12 4:40am    
Nice and effective answer as always. My +5.
Abhinav S 2-Jul-12 4:53am    
Thank you.
Hi,
You can try this:
C#
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
 
Share this answer
 
Yes you can. asuming the query string name is "id", and the type you are expecting is string. you can do this


C#
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
}
 
Share this answer
 
check the querystring length
C#
if (Request.QueryString.ToString().Length >0)
{
//Request.QueryString is exist
} 
 
Share this answer
 
C#
Uri uri = new Uri(Request.RawUrl);
if(uri.Query.Length>0)
 
Share this answer
 
Hi Msaya,

Sure you can. It goes something like this:

C#
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
 
Share this answer
 
if (Request.QueryString.ToString().Contains("test"))
{
your code;
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900