7

How to check if the web page contains any string queries at the page load?

1
  • Do you mean QueryString? Commented Jul 17, 2009 at 6:44

4 Answers 4

18

You can determine if there are any values in the QueryString by checking its count:

Request.QueryString.Count > 0;

That said if you are trying to prevent a page from erroring because you don't want to access a value that is not there I recommend wrapping query parms up in page properties and returning safe values from the property.

As an example

// setting this as protected makes it available in markup
protected string TaskName
{
    get { return (string)Request.QueryString["VarName"] ?? String.Empty; }
}
Sign up to request clarification or add additional context in comments.

Comments

18

Check for

Request.QueryString["QueryStringName"]

if you know the particular name and it returns null if there isn't any querystring by that name

or if you want to check the count of querystrings then

Request.QueryString.Count

and check against 0. If greater than 0 then there is atleast 1 string appended.

Comments

1

To check if the page was accessed with any query string, you can check the Count property:

bool expression = Request.QueryString.Count > 0;

To access a defined query string parameter, you can do it like this:

string myParam = Request.QueryString["MyParam"];

myParam will be null if it is not on the URL.

1 Comment

But there isn't any length property
0
if(Request.QueryString.Count > 0)
{
    //Code here
}
else
{
  //Code here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.