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

<p>They can select a report using two methods. With <code>SelectedReport=MyReport</code> 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.</p>

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

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

<p><strong>Edit:</strong> And I also need to preserve ViewState.</p>

<p>I've tried doing <code>Server.Transfer(Request.Path + "?SelectedReport=" + SelectedReport, true)</code> 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.</p>

<p>Maybe I'm asking the impossible or going about it completely the wrong way. </p>

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

[{'answer_id': 26902, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 26908, 'author': 'Craig', 'author_id': 2894, 'author_profile': 'https://Stackoverflow.com/users/2894', 'pm_score': 0, 'selected': False, 'text': '<p>Have you tried to modify the Request.QueryString[] on the SelectedIndexChanged for the DropDown?  That should do the trick.</p>\n'}, {'answer_id': 26938, 'author': 'Kevin Pang', 'author_id': 1574, 'author_profile': 'https://Stackoverflow.com/users/1574', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 26943, 'author': 'Jason Bunting', 'author_id': 1790, 'author_profile': 'https://Stackoverflow.com/users/1790', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>EDIT: Here is some quick and dirty code that is indicative of what would do the trick:</p>\n\n<pre><code>&lt;script&gt;\n    function changeReport(dropDownList) {\n        var selectedReport = dropDownList.options[dropDownList.selectedIndex];\n        window.location = ("scratch.htm?SelectedReport=" + selectedReport.value);\n    }\n&lt;/script&gt;\n\n&lt;select id="SelectedReport" onchange="changeReport(this)"&gt;\n    &lt;option value="foo"&gt;foo&lt;/option&gt;\n    &lt;option value="bar"&gt;bar&lt;/option&gt;\n    &lt;option value="baz"&gt;baz&lt;/option&gt;\n&lt;/select&gt;\n</code></pre>\n\n<p>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).</p>\n\n<p><strong>@Ray:</strong> You use ViewState?! I\'m so sorry. :P Why, in this instance, do you need to preserve it. pray tell?</p>\n'}, {'answer_id': 29605, 'author': 'Tom', 'author_id': 3139, 'author_profile': 'https://Stackoverflow.com/users/3139', 'pm_score': 0, 'selected': False, 'text': '<p>The view state only lasts for <a href="http://msdn.microsoft.com/en-us/library/540y83hx.aspx" rel="nofollow noreferrer">multiple requests for the same page</a>. Changing the query string in the URL is requesting a new page, thus clearing the view state.</p>\n\n<p>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.</p>\n\n<p>Another option is to use the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.action.aspx" rel="nofollow noreferrer">Action property</a> 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.</p>\n\n<pre><code>Form.Action = Request.Path;\n</code></pre>\n'}, {'answer_id': 16968178, 'author': 'Rajat Agrawal', 'author_id': 2460674, 'author_profile': 'https://Stackoverflow.com/users/2460674', 'pm_score': 0, 'selected': False, 'text': '<p>You can use the following function to modify the querystring on postback in asp.net using the Webresource.axd script as below.</p>\n\n<pre><code>var url = updateQueryStringParameter(window.location.href,\n                                     \'Search\',\n                                     document.getElementById(\'txtSearch\').value);\nWebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("searchbutton", "",\n                                  true, "aa", url, false, true));\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/26882', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/233/']