views:

6304

answers:

6

My asp.net page will render different controls based on which report a user has selected e.g. some reports require 5 drop downs, some two checkboxes and 6 dropdowns).

They can select a report using two methods. With SelectedReport=MyReport in the query string, or by selecting it from a dropdown. And it's a common case for them to come to the page with SelectedReport in the query string, and then change the report selected in the drop down.

My question is, is there anyway of making the dropdown modify the query string when it's selected. So I'd want SelectedReport=MyNewReport in the query string and the page to post back.

At the moment it's just doing a normal postback, which leaves the SelectedReport=MyReport in the query string, even if it's not the currently selected report.

Edit: And I also need to preserve ViewState.

I've tried doing Server.Transfer(Request.Path + "?SelectedReport=" + SelectedReport, true) in the event handler for the Dropdown, and this works function wise, unfortunately because it's a Server.Transfer (to preserve ViewState) instead of a Response.Redirect the URL lags behind what's shown.

Maybe I'm asking the impossible or going about it completely the wrong way.

@Craig The QueryString collection is read-only and cannot be modified.
@Jason That would be great, except I'd lose the ViewState wouldn't I? (Sorry I added that after seeing your response).

A: 

If it's an automatic post when the data changes then you should be able to redirect to the new query string with a server side handler of the dropdown's 'onchange' event. If it's a button, handle server side in the click event. I'd post a sample of what I'm talking about but I'm on the way out to pick up the kids.

Chuck
A: 

Have you tried to modify the Request.QueryString[] on the SelectedIndexChanged for the DropDown? That should do the trick.

Craig
A: 

You could populate your dropdown based on the querystring on non-postbacks, then always use the value from the dropdown. That way the user's first visit to the page will be based on the querystring and subsequent changes they make to the dropdown will change the selected report.

Kevin Pang
+2  A: 

You need to turn off autopostback on the dropdown - then, you need to hook up some javascript code that will take over that role - in the event handler code for the onchange event for the dropdown, you would create a URL based on the currently-selected value from the dropdown and use javascript to then request that page.

EDIT: Here is some quick and dirty code that is indicative of what would do the trick:

<script>
    function changeReport(dropDownList) {
     var selectedReport = dropDownList.options[dropDownList.selectedIndex];
     window.location = ("scratch.htm?SelectedReport=" + selectedReport.value);
    }
</script>

<select id="SelectedReport" onchange="changeReport(this)">
    <option value="foo">foo</option>
    <option value="bar">bar</option>
    <option value="baz">baz</option>
</select>

Obviously you would need to do a bit more, but this does work and would give you what it seems you are after. I would recommend using a JavaScript toolkit (I use MochiKit, but it isn't for everyone) to get some of the harder work done - use unobtrusive JavaScript techniques if at all possible (unlike what I use in this example).

@Ray: You use ViewState?! I'm so sorry. :P Why, in this instance, do you need to preserve it. pray tell?

Jason Bunting
A: 

The view state only lasts for multiple requests for the same page. Changing the query string in the URL is requesting a new page, thus clearing the view state.

Is it possible to remove the reliance on the view state by adding more query string parameters? You can then build a new URL and Response.Redirect to it.

Another option is to use the Action property on the form to clear the query string so at least the query string does not contradict what's displayed on the page after the user selects a different report.

Form.Action = Request.Path;
Tom
A: 

Thanks! You did it!!! I didnt have the autopostback set to false for the dropdownlist and thats what was causing problems.