Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postback with Modified Query String from Dropdown in ASP.NET

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).

like image 891
Ray Avatar asked Dec 15 '25 21:12

Ray


1 Answers

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?

like image 199
Jason Bunting Avatar answered Dec 18 '25 09:12

Jason Bunting



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!