سؤال

For example, in Login.aspx , I have two link.

<a href="Main.aspx?site=facebook">Facebook_link</a>
<a href="Main.aspx">Google_link</a> // (I wouldn't like to wirte "Main.aspx?site=google"

On Main Page_Load , there is :

if(Request.QueryString["site"].ToString()!="facebook")
{
.....
}

On Login page if I click Facebook_link and then I go to Main page, everything works great.

But on Login page if I click Google_link and then I go to Main page , I get error. [Error: Object reference not set to an instance of an object]

I know why I get this error.

I woud like to ask that it is possible to check on Main page that "If Request.QueryString["site"] is exist, do this".

هل كانت مفيدة؟

المحلول

Request.QueryString["site"] is probably evaluating to null.

To prevent this happening, you need to check for null like this:

var queryString = Request.QueryString["site"];

if (queryString != null && queryString.ToString() != "facebook")
{    .....
}

نصائح أخرى

Try this:

if (Request.QueryString["site"]!= null && Request.QueryString["site"]!= "facebook")
{    .....
}

QueryString is like session, hidden fields. Once you set a value it will hold it untill you change it. you have told that you click facebook link. so querystring will hold the value "facebook" even you click on google link. because you haven't set any values on google. So just do like this,

<a href="Main.aspx?site=google">Google_link</a>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top