Querystring validation

E

EW Newbie

My apologies in advance if there is a better forum for this question.

Given the following snippet:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Request.QueryString.Count > 0 )
{
if ( Request.QueryString[ "pid" ] != "" )
{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QueryString[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?
 
M

Mark Rae [MVP]

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?

protected void Page_Load(Object Sender, EventArgs e)
{
if (Request.QueryString["pid"] != null)
{
// do something
}
}
 
G

Guest

My apologies in advance if there is a better forum for this question.

Given the following snippet:

   <script type="text/c#" runat="server">
   protected void Page_Load(Object Sender, EventArgs e)
   {
      if ( Request.QueryString.Count > 0 )
        {
           if ( Request.QueryString[ "pid" ] != "" )
             {

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QueryString[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?

if ( Request.QueryString != null && Request.QueryString["pid"] != "" )
{
...
}
 
M

Milosz Skalecki [MCAD]

There's even better way of handling optional query string parameters which i
prefer ( i assume pid's type is integer):

protected int? Pid
{
get
{
int value;
return int.TryParse(Request.QueryString["pid"], out value) ?
(int?)value : null;
}
}

and then you can easly use it everywhere in the code:

if (Pid.HasValue)
{
// do something here
}

or if the value is optional but should be initialized to a default value:

private int? pid;
protected int Pid
{
get
{
if (!this.pid.HasValue)
{
int value;
this.pid = int.TryParse(Request.QueryString["pid"], out value) ?
value : 0 // replace it with default value ;
}
return this.pid.Value;
}
}

I know it mihgt look horrible but if you parse the same parameter
many times on the same page it should help you clean up the code a little bit.

Regards
--
Milosz


Milosz Skalecki said:
if (!String.IsNullOrEmpty(Request.QueryString["pid"])
{
}

Regards
--
Milosz


EW Newbie said:
My apologies in advance if there is a better forum for this question.

Given the following snippet:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Request.QueryString.Count > 0 )
{
if ( Request.QueryString[ "pid" ] != "" )
{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QueryString[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?
 
E

EW Newbie

Thanks guys for the help so far.

The situation is this: page has a dropdownlistbox. Under normal usage, the
visitor loads the page, selects an item from the list, autopostback occurs,
page is reloaded. Script code notices that listbox now has a .Selected value
and causes a dataview to be populated and displayed on the page.

I hope that makes sense. :)

For other purposes (search engines, mostly), I would like to call the page
with a querystring which forces the .Selected on the listbox and immediately
loads the dataview.

So, on PageLoad, the script must determine if this is a first load with no
querystring (in which case display the listbox with default "Select..."
text), a postback (in which case determine the .Selected value and load the
dataview accordingly), or a valid querystring (in which case set the
..Selected and load the data view).

I had this working perfectly but wanted to add one more test (isn't that
always the way??) to clean up the action when a pid querystring is present
but the pid value does not exist in the listitems collection.

As it stands now, the script looks somewhat like this:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Page.IsPostBack != true && Request.QueryString.Count > 0 &&
Request.QueryString[ "pid" ] != null )
{
string qstring = Request.QueryString[ "pid" ];
ListItem lstitem = DropDownList1.Items.FindByValue(qstring);
if ( lstitem != null )
{
//do stuff
}
else
{
//do stuff
}
}
else
{
//do stuff
}
}
</script>

The problem is, the "if ( lstitem != null )" always fails (that is, lstitem
is always null), even when the value of qstring is absoultely present in the
listitems collection.

How come?




Milosz Skalecki said:
if (!String.IsNullOrEmpty(Request.QueryString["pid"])
{
}

Regards
--
Milosz


EW Newbie said:
My apologies in advance if there is a better forum for this question.

Given the following snippet:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Request.QueryString.Count > 0 )
{
if ( Request.QueryString[ "pid" ] != "" )
{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QueryString[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?
 
E

EW Newbie

Oops! Obviously, the control wasn't yet databound...



EW Newbie said:
Thanks guys for the help so far.

The situation is this: page has a dropdownlistbox. Under normal usage, the
visitor loads the page, selects an item from the list, autopostback occurs,
page is reloaded. Script code notices that listbox now has a .Selected value
and causes a dataview to be populated and displayed on the page.

I hope that makes sense. :)

For other purposes (search engines, mostly), I would like to call the page
with a querystring which forces the .Selected on the listbox and immediately
loads the dataview.

So, on PageLoad, the script must determine if this is a first load with no
querystring (in which case display the listbox with default "Select..."
text), a postback (in which case determine the .Selected value and load the
dataview accordingly), or a valid querystring (in which case set the
.Selected and load the data view).

I had this working perfectly but wanted to add one more test (isn't that
always the way??) to clean up the action when a pid querystring is present
but the pid value does not exist in the listitems collection.

As it stands now, the script looks somewhat like this:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Page.IsPostBack != true && Request.QueryString.Count > 0 &&
Request.QueryString[ "pid" ] != null )
{
string qstring = Request.QueryString[ "pid" ];
ListItem lstitem = DropDownList1.Items.FindByValue(qstring);
if ( lstitem != null )
{
//do stuff
}
else
{
//do stuff
}
}
else
{
//do stuff
}
}
</script>

The problem is, the "if ( lstitem != null )" always fails (that is, lstitem
is always null), even when the value of qstring is absoultely present in the
listitems collection.

How come?




Milosz Skalecki said:
if (!String.IsNullOrEmpty(Request.QueryString["pid"])
{
}

Regards
--
Milosz


EW Newbie said:
My apologies in advance if there is a better forum for this question.

Given the following snippet:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Request.QueryString.Count > 0 )
{
if ( Request.QueryString[ "pid" ] != "" )
{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QueryString[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?
 
M

Milosz Skalecki [MCAD]

Hi there,

Seems you're using SqlDataSource/ObjectDataSource to populate the drop down
list. Automatic population takes place in Page.OnPreRender method
(simplifying), therefore Items are not available yet in Page_Load handler
(this situation takes place only if the page is requested for the first time
and as the statement in your code Page.IsPostBack != true indicates that's
the case). In order to fix the problem you have to make sure DropDownList has
been populated (by calling DataBind) before accessing its items:

string qstring = Request.QueryString[ "pid" ];
DropDownList1.DataBind();
ListItem lstitem = DropDownList1.Items.FindByValue(qstring);

Once again, i'm assuming you're using SqlDataSource/ObjectDataSource
controls to populate drop down.

Hope this helps
--
Milosz


EW Newbie said:
Thanks guys for the help so far.

The situation is this: page has a dropdownlistbox. Under normal usage, the
visitor loads the page, selects an item from the list, autopostback occurs,
page is reloaded. Script code notices that listbox now has a .Selected value
and causes a dataview to be populated and displayed on the page.

I hope that makes sense. :)

For other purposes (search engines, mostly), I would like to call the page
with a querystring which forces the .Selected on the listbox and immediately
loads the dataview.

So, on PageLoad, the script must determine if this is a first load with no
querystring (in which case display the listbox with default "Select..."
text), a postback (in which case determine the .Selected value and load the
dataview accordingly), or a valid querystring (in which case set the
.Selected and load the data view).

I had this working perfectly but wanted to add one more test (isn't that
always the way??) to clean up the action when a pid querystring is present
but the pid value does not exist in the listitems collection.

As it stands now, the script looks somewhat like this:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Page.IsPostBack != true && Request.QueryString.Count > 0 &&
Request.QueryString[ "pid" ] != null )
{
string qstring = Request.QueryString[ "pid" ];
ListItem lstitem = DropDownList1.Items.FindByValue(qstring);
if ( lstitem != null )
{
//do stuff
}
else
{
//do stuff
}
}
else
{
//do stuff
}
}
</script>

The problem is, the "if ( lstitem != null )" always fails (that is, lstitem
is always null), even when the value of qstring is absoultely present in the
listitems collection.

How come?




Milosz Skalecki said:
if (!String.IsNullOrEmpty(Request.QueryString["pid"])
{
}

Regards
--
Milosz


EW Newbie said:
My apologies in advance if there is a better forum for this question.

Given the following snippet:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Request.QueryString.Count > 0 )
{
if ( Request.QueryString[ "pid" ] != "" )
{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QueryString[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?
 
E

EW Newbie

I am using an XML datasource and, yes, databind did the trick.

Thanks for the help!


Milosz Skalecki said:
Hi there,

Seems you're using SqlDataSource/ObjectDataSource to populate the drop down
list. Automatic population takes place in Page.OnPreRender method
(simplifying), therefore Items are not available yet in Page_Load handler
(this situation takes place only if the page is requested for the first time
and as the statement in your code Page.IsPostBack != true indicates that's
the case). In order to fix the problem you have to make sure DropDownList has
been populated (by calling DataBind) before accessing its items:

string qstring = Request.QueryString[ "pid" ];
DropDownList1.DataBind();
ListItem lstitem = DropDownList1.Items.FindByValue(qstring);

Once again, i'm assuming you're using SqlDataSource/ObjectDataSource
controls to populate drop down.

Hope this helps
--
Milosz


EW Newbie said:
Thanks guys for the help so far.

The situation is this: page has a dropdownlistbox. Under normal usage, the
visitor loads the page, selects an item from the list, autopostback occurs,
page is reloaded. Script code notices that listbox now has a .Selected value
and causes a dataview to be populated and displayed on the page.

I hope that makes sense. :)

For other purposes (search engines, mostly), I would like to call the page
with a querystring which forces the .Selected on the listbox and immediately
loads the dataview.

So, on PageLoad, the script must determine if this is a first load with no
querystring (in which case display the listbox with default "Select..."
text), a postback (in which case determine the .Selected value and load the
dataview accordingly), or a valid querystring (in which case set the
.Selected and load the data view).

I had this working perfectly but wanted to add one more test (isn't that
always the way??) to clean up the action when a pid querystring is present
but the pid value does not exist in the listitems collection.

As it stands now, the script looks somewhat like this:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Page.IsPostBack != true && Request.QueryString.Count > 0 &&
Request.QueryString[ "pid" ] != null )
{
string qstring = Request.QueryString[ "pid" ];
ListItem lstitem = DropDownList1.Items.FindByValue(qstring);
if ( lstitem != null )
{
//do stuff
}
else
{
//do stuff
}
}
else
{
//do stuff
}
}
</script>

The problem is, the "if ( lstitem != null )" always fails (that is, lstitem
is always null), even when the value of qstring is absoultely present in the
listitems collection.

How come?




Milosz Skalecki said:
if (!String.IsNullOrEmpty(Request.QueryString["pid"])
{
}

Regards
--
Milosz


:

My apologies in advance if there is a better forum for this question.

Given the following snippet:

<script type="text/c#" runat="server">
protected void Page_Load(Object Sender, EventArgs e)
{
if ( Request.QueryString.Count > 0 )
{
if ( Request.QueryString[ "pid" ] != "" )
{

My problem is as follows,

a) if url is passed with no querystring at all, works as intended
b) if url is passed with querystring "?pid=" (blank), works as intended
c) if url is passed with querystring "?pid=12345", works as intended
d) if url is passed with querystring with misspelled or missing "pid"
element, the script crashes because the [ if ( Request.QueryString[ "pid" ]
!= "" ) ] returns true.

How do I test to ensure that an element ("pid") is present in the
querystring before continuing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top