views:

168

answers:

1

Why is "_requestValueCollection" empty on PostBack?

I have a really strange problem with post backs. In some cases on post backs (this.Request.RequestType == "POST") have null "_requestValueCollection" member. And for ASP.NET that means this.IsPostBack == false.

So I have modified the Page_Load in the following way:

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!this.IsPostBack && this.Request.RequestType != "POST")
        {
           //REGULAR INIT STUFF

        }
        else
        {
           //REGULAR SITE POSTBACK STUFF

        }
    }

What is possible danger of this approach? So far everything is doing OK (and is pretty rich and complicated page).

+2  A: 

It isn't clear from your example what you are attempting to do with this code, so this is mostly a short in the dark.

You probably don't need the second part of the if statement. Checking IsPostBack alone should be sufficient.

_requestValueCollection is not a property, it is a field and probably isn't a good place to get at the data submitted by the client. I suggest instead that you consider using the Form property (this.Request.Form) or the Headers property (this.Request.Headers) depending on what you are looking for. Keep in mind that most of the time you can just get form values from the asp.net controls on the form directly.

You may also want to look at the Request.HttpMethod property if you need to determine the exact http method used to invoke the page.


Edit: Adding info about _requestValueCollection

The mechanics behind the _requestValueCollection being loaded are quite complex, but I took a look at the MS source and from what I can determine the page calls on every control on the page that implements the IPostBackDataHandler interface. For each of these it will call the LoadPostData method which adds the data for that control to the collection.

The main things that I can think of off the top of my head that might cause the collection to be null would be:

  • no server controls on the page implement IPostBackDataHandler
  • there is no server form, or the contents of the form weren't sent by the client
  • Alternately, the page may be using query strings to convey the data to the server, and the query string doesn't contain anything

As I said, this is a bit fuzzy. The Page class is very complex internally and so there could be other ways data gets put into that collection too, but this was all I could find on a casual examination.

Stephen M. Redd
"_requestValueCollection is not a property" Yeah sorry my mistake, is a private member to be specific.
Peter Stegnar
so is this your question: Why requestValueCollection is empty on postback?
Neil
Yeah! This is my fist sentence in question ...
Peter Stegnar
Your question talks about the field, but the code doesn't show any usage related to it. The field is private, so you can't directly access it from the page load event handler code. What code are you using to get at that collection?
Stephen M. Redd
This is private ASP.NET member. But based on that member and a couple of others ASP.NET engine indicates whether is PostBack on not. To answer this question, I believe deep ASP.NET knowledge is required. Basically the question is, who sets "_requestValueCollection" to "null".
Peter Stegnar
I edited the answer to include more info about the _requestValueCollection.
Stephen M. Redd