Querystring validation

Discussion in 'ASP .Net' started by EW Newbie, Jan 18, 2008.

  1. EW Newbie

    EW Newbie 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?
     
    EW Newbie, Jan 18, 2008
    #1
    1. Advertising

  2. "EW Newbie" <> wrote in message
    news:...

    > 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
    }
    }


    --
    Mark Rae
    ASP.NET MVP
    http://www.markrae.net
     
    Mark Rae [MVP], Jan 18, 2008
    #2
    1. Advertising

  3. On Jan 18, 3:54 pm, EW Newbie <>
    wrote:
    > 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"] != "" )
    {
    ...
    }
     
    Alexey Smirnov, Jan 18, 2008
    #3
  4. if (!String.IsNullOrEmpty(Request.QueryString["pid"])
    {
    }

    Regards
    --
    Milosz


    "EW Newbie" wrote:

    > 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?
    >
    >
    >
    >
    >
     
    Milosz Skalecki [MCAD], Jan 18, 2008
    #4
  5. 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 [MCAD]" wrote:

    > if (!String.IsNullOrEmpty(Request.QueryString["pid"])
    > {
    > }
    >
    > Regards
    > --
    > Milosz
    >
    >
    > "EW Newbie" wrote:
    >
    > > 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?
    > >
    > >
    > >
    > >
    > >
     
    Milosz Skalecki [MCAD], Jan 18, 2008
    #5
  6. EW Newbie

    EW Newbie Guest

    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 [MCAD]" wrote:

    > if (!String.IsNullOrEmpty(Request.QueryString["pid"])
    > {
    > }
    >
    > Regards
    > --
    > Milosz
    >
    >
    > "EW Newbie" wrote:
    >
    > > 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?
    > >
    > >
    > >
    > >
    > >
     
    EW Newbie, Jan 18, 2008
    #6
  7. EW Newbie

    EW Newbie Guest

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



    "EW Newbie" wrote:

    > 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 [MCAD]" wrote:
    >
    > > if (!String.IsNullOrEmpty(Request.QueryString["pid"])
    > > {
    > > }
    > >
    > > Regards
    > > --
    > > Milosz
    > >
    > >
    > > "EW Newbie" wrote:
    > >
    > > > 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?
    > > >
    > > >
    > > >
    > > >
    > > >
     
    EW Newbie, Jan 18, 2008
    #7
  8. 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" wrote:

    > 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 [MCAD]" wrote:
    >
    > > if (!String.IsNullOrEmpty(Request.QueryString["pid"])
    > > {
    > > }
    > >
    > > Regards
    > > --
    > > Milosz
    > >
    > >
    > > "EW Newbie" wrote:
    > >
    > > > 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?
    > > >
    > > >
    > > >
    > > >
    > > >
     
    Milosz Skalecki [MCAD], Jan 18, 2008
    #8
  9. EW Newbie

    EW Newbie Guest

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

    Thanks for the help!


    "Milosz Skalecki [MCAD]" wrote:

    > 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" wrote:
    >
    > > 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 [MCAD]" wrote:
    > >
    > > > if (!String.IsNullOrEmpty(Request.QueryString["pid"])
    > > > {
    > > > }
    > > >
    > > > Regards
    > > > --
    > > > Milosz
    > > >
    > > >
    > > > "EW Newbie" wrote:
    > > >
    > > > > 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?
    > > > >
    > > > >
    > > > >
    > > > >
    > > > >
     
    EW Newbie, Jan 19, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Mackay
    Replies:
    0
    Views:
    2,701
    Colin Mackay
    Jun 25, 2003
  2. Libs
    Replies:
    0
    Views:
    1,524
  3. Colin Basterfield

    Web form validation vs object validation

    Colin Basterfield, Nov 28, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    433
    Tommy
    Nov 29, 2003
  4. Mehdi
    Replies:
    6
    Views:
    36,179
    sloan
    Apr 6, 2006
  5. Adeel Ahmad
    Replies:
    1
    Views:
    564
    Anthony Jones
    Mar 7, 2006
Loading...

Share This Page