none
What is the default search behavior of an asp.net MVC SelectListItem control on keypress? RRS feed

  • Question

  • I have a dropdownlist control created using the asp.net MVC SelectListItem base control having the following list items (in the following order):

    AMERICA
    AFGHANISTAN
    ALGERIA

    With the focus on the control, when I press the key 'A', the control selects AFGHANISTAN. I am assuming that it is selected because it comes first in alphabetical order. Is this assumption true? If yes, is there any way to change this behavior and select the item on the basis of the display order and not alphabetical order i.e. AMERICA?

    Using the following code inside my wrapper control class:

    selectList = new SelectList(tableData,
                    valueExpression, 
                    descExpression,
                    selectedValue);

    Expectation is that the control should select the first matching item on the basis of the display order and not alphabetical order.

    Any documentation around the filtering functionality of SelectListItem would be helpful.

    • Moved by CoolDadTx Wednesday, July 10, 2019 2:09 PM ASP.NET related
    Tuesday, July 9, 2019 7:09 AM

All replies

  • ASP.NET MVC has a forum in ASP.NET forums that you can post to for help.

    https://forums.asp.net/

    Tuesday, July 9, 2019 10:28 AM
  • Hi deehar,

    Thank you for posting here.

    According to your description, if you want to select the option based on custom filter rule. I suggest you could try to use jquery’s keydown to achieve your requirement.

    Details ,you could refer to below codes:

    $("#ID").keydown(function(e){ // the keydown even on ul
        var chr =String.fromCharCode( e.which );
        $(this).find("li").each(function(){ // if the key down is not in UL then make sure the "this" should refer to UL
          var name = $(this).text();
            if( name.toLowerCase().indexOf(chr.toLowerCase()) === 0 ) {
                $(this).focus(); // this should focus you
                return false; 
            }
       });
    

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 10, 2019 5:09 AM