ASP.NET MVC ListBox - Reset Selection

  • Zimbabwe230
  • Zambia229
  • Yemen228
  • Wallis and Futuna227
  • Wales226
  • Virgin Islands225
  • Vietnam224
  • Venezuela223
  • Vatican City222
  • Vanuatu221
  • Uzbekistan220
  • USA219
  • Uruquay218
  • United Arab Emirates217
  • Ukraine216
 
Reset
View - Aspx

    <% Html.Obout(new ListBox("ListBox1")
    {   
        SelectedIndex = 3
    }); %>
    
    <% Html.Obout(new OboutButton("OboutButton1")
    {
        OnClientClick = "return onReset()"
    }); %>	   
    
    
View - Razor

    @Html.Obout(new ListBox("ListBox1")
    {   
        SelectedIndex = 3
    })
    
    @Html.Obout(new OboutButton("OboutButton1")
    {
        OnClientClick = "return onReset()"
    })
    
    
JavaScript

    var initialSelectedIndex = -1;
    window.onload = function () {
        initialSelectedIndex = ListBox1.selectedIndex();

    }

    function onReset() {
        ListBox1.selectedIndex(initialSelectedIndex);
        return false;
    }
    
    
This example shows how to reset the selection from the ListBox to the initial selection.

The initial selected index was cached in a client-side variable when the page was loaded and then it is used as a parameter for the selectedIndex method when the Reset button is clicked.