sazr
sazr

Reputation: 25928

Accessing Request.QueryString[foo] is null but the URL shows the query string/parameter is right there

I am calling an .aspx script through AJAX. In that script I am attempting to get a value from the query string using Request.QueryString["i"] but it always returns null even though, if I examine the Request object in debug mode, the query string IS right there.

Whats going wrong & how can I retrieve the i parameter value from testScript.aspx?i=199?

Heres my simple code:

    public partial class getData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            short index = System.Convert.ToInt16(Request.QueryString["i"]);  // BREAKPOINT
        }
    }

When I use a break point & examine the Request Object I can see that the Request.QueryString variable is empty(just a {}). Request.QueryString["i"] is null.

If you look at the following img you can see the form has my i parameter(thats my query string .aspx?i=4

enter image description here

Upvotes: 1

Views: 9575

Answers (3)

Surendra Tarai
Surendra Tarai

Reputation: 45

Hello Jake u are used post method, and in post method query string not worked, so u have to user either get method to use query string or if u are used post method then change your code in testScript.aspx load event Request.QueryString to Request.Form like this

public partial class getData : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        short index = System.Convert.ToInt16(Request.Form["i"]);  // BREAKPOINT
    }
}

Upvotes: 0

Rob Levine
Rob Levine

Reputation: 41318

I think you might be confusing the query string with form fields. In the screenshot, your value "i" is clearly on the Form property.

Form fields are fields on the page which are POSTed, as opposed to the query string items, which appear at the end of the url.

If you look at the QueryString property, I suspect you won't find your item.

Try using:

Request.Form["i"]

Upvotes: 1

Pavel Krymets
Pavel Krymets

Reputation: 6293

Your form is sent using POST request, and parameter i is not in QueryString but in request body encoded using multipart form data format,Request.QueryString only show's parameters passed through URI like page.asax?i=4. Use Request.Form["i"]

Upvotes: 9

Related Questions