views:

311

answers:

2

Is there anyway to do a postback with a new querystring that doesn't reset all of the controls on a page to their defaults? I have a page named "default.aspx" with several checkbox controls on it. I can check them all I want and they will remain persistent (meaning they keep their checked or unchecked state) on postbacks to "default.aspx". However, if I postback to "default.aspx?page=2", the controls all revert to their default state. Is there a way to keep them from doing that?

+2  A: 

The thing to remember here is that every time you do a new postback, you're working with a brand new instance of your page class. The old instance was discarded the moment it was sent to the web browser. With that in mind, the fact that state can be persisted between postbacks at all is pretty amazing. There's a lot that has to happen to make it all sync up okay.

One of those things is ViewState. ViewState is a special hidden input element in your page's form. When you request your page with a new query string, you're no longer POSTing that viewstate field from the form. You're not really doing a postback at all anymore: it's a request for a whole new page. There is no POST data from any form, and therefore the ViewState data is missing and ASP.Net has no clue about anything you might have done previously.

Joel Coehoorn
I see pages all the time that have a "page 2" link and post back to themselves without resetting textboxes or checkboxes. I guess I could write somethig to a text file and read the text file the next time the page loads, but I can't believe that is how it is actually done.
Tom V
If they're actually "posting back" then they're form submissions and the querystring couldn't be changing. There's nothing stopping you from developing your page this way but you would be responsible for compensating for the loss of PostBacks.
Ken Browning
You mean I should be using the POST method rather the GET method? I can do that, except I don't have a form. I was expecting the ASP.NET controls to replace the need for a form. I should have written this in Pearl, or PHP, maybe. I'm definitely not sold on .NET yet.
Tom V
No, I don't mean to use the post method. A PostBack in asp.net is a very specific term. This answer is explaining that asp.net page state is persisted via the ViewState/PostBack model which is mutually exclusive with changing the querystring.
Ken Browning
A: 

How are you posting back when you change the querystring? If you're using ASP.net checkbox controls (with runat="server") then they should be persisted with the viewstate.

Joel is correct that a new instance of the Page class is created, but that is the purpose of Viewstate, to overcome the stateless nature of HTTP.

nexus
In the code behind, I have added: Response.Write("<a href="default.aspx?page=2> Page 2 </a>). It takes me page 2 of ALL my records, not the records that were selected by the controls.
Tom V
That's your problem then. The way the viewstate works -- and therefore the way the state of the controls is persistent -- is by posting back the page. By creating a link you're circumventing that entire process. Try creating and ASP control and setting its onclick to your method handling your paging
nexus
I tried doing that, actually. But the method I set never got called. I also tried setting my "OnCheckedChanged" property (for my checkboxes) to a method that would change the PostbackURL of the button. But that method never got called, either, even though I was clicking the checkbox with both hands.
Tom V
I'll say this as well: Any use of Response.Write() should be a rare occurence in ASP.Net code.
Joel Coehoorn