New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Back Button and SelectedValue
PROBLEM
Steps to reproduce the problem:
-
Select a combo item
-
Use Response.Redirect or Server.Transfer to go to another page
-
Click on the Back button of the browser
The combobox shows the correct text, but the SelectedValue property is incorrect (the SelectedItem is lost).
WORKAROUND
First, add a hidden field to the page that will hold the selected value of the combobox:
HTML
<input type="hidden" id="RadComboBox1Value" value="" />
After that, subscribe to the OnClientSelectedIndexChanged event and update that hidden field:
JavaScript
function onSelectedIndexChanged(sender, eventArgs) {
$get("RadComboBox1Value").value = eventArgs.get_item().get_value();
}
In the end, on pageLoad()
check for the value of the hidden field. If it is not empty - find the appropriate combo item and select it:
JavaScript
function pageLoad() {
var savedValue = $get("RadComboBox1Value").value;
var combo = $find('<%= RadComboBox1.ClientID %>');
if (savedValue != "" && combo.findItemByValue(savedValue)) {
combo.findItemByValue(savedValue).select();
}
````
Here is the final code:
````ASP.NET
<telerik:RadComboBox RenderMode="Lightweight" ID="RadComboBox1"
runat="server"
OnClientSelectedIndexChanged="onSelectedIndexChanged">
<Items>
<telerik:RadComboBoxItem runat="server" Text="Red" Value="red" />
<telerik:RadComboBoxItem runat="server" Text="Blue" Value="blue" />
<telerik:RadComboBoxItem runat="server" Text="Green" Value="Green" />
</Items>
</telerik:RadComboBox>
<input type="hidden" id="RadComboBox1Value" value="" />
<asp:Button ID="Button1" runat="server" Text="Button that Redirects" />
JavaScript
function pageLoad() {
var savedValue = $get("RadComboBox1Value").value;
var combo = $find('<%= RadComboBox1.ClientID %>');
if (savedValue != "" && combo.findItemByValue(savedValue)) {
combo.findItemByValue(savedValue).select();
}
}
function onSelectedIndexChanged(sender, eventArgs) {
$get("RadComboBox1Value").value = eventArgs.get_item().get_value();
}