Enter Key Press When No Item Selected

4 posts, 0 answers
  1. Jon Shipman
    Jon Shipman avatar
    43 posts
    Member since:
    Nov 2009

    Posted 06 Mar 2011 Link to this post

    I have a RadComboBox.  AllowCustomText is enabled to allow filtering.  When a user presses the Enter key it fires the OnSelectedIndexChanged event.  I don't want the event to fire unless an item is selected.  Unless an item is selected I don't want anything to happen.

    How can I do this?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 08 Mar 2011 Link to this post

    Hello Jon,
    You can achieve this by canceling the event as shown below.
    javascript:
      function pageLoad()
      {
            var combo = $find("<%= RadComboBox1.ClientID %>");//acessing the combobox
            var input = combo.get_inputDomElement();
            input.attachEvent("onkeydown", onKeyDownHandler);//attaching the onkeydown event
      }
        function onKeyDownHandler(e)
      {
             if (!e)
                e = window.event;
            var code = e.keyCode;
            if (code == 13)//cancel the event if the entered key is EnterKey
             {
                e.cancelBubble = true;
                if (e.stopPropagation)
                {
                    e.stopPropagation();
                }
                return false;
              }
      }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jon Shipman
    Jon Shipman avatar
    43 posts
    Member since:
    Nov 2009

    Posted 15 Mar 2011 Link to this post

    Hey Shinu,

    Thanks for your reply.  This certainly does prevent the Enter key from raising the event.  But if a user has typed a few letters and found some matching items, then used the down arrow to highlight an item and then pressed Enter, I want the event to be raised.

    Here's the approach I used...  Add the OnClientSelectedIndexChanging handler to the RadComboBox.

    <telerik:RadComboBox ID="RadComboBox1"
        runat="server"
        AllowCustomText="true"
        EmptyMessage="Search Titles..."
        Filter="Contains"
        DataTextField="Title"
        DataValueField="ID"
        AutoPostBack="true"
        DataSourceID="sdsTitles"
        EnableAutomaticLoadOnDemand="True"
        ItemsPerRequest="15"
        ShowMoreResultsBox="true"
        OnClientSelectedIndexChanging="OnSelectedIndexChanging"
        OnSelectedIndexChanged="RadComboBox1_SelectedIndexChanged" >
    </telerik:RadComboBox>


    Add the client event handler: If the eventArgs._item is null it cancels the event.
    function OnSelectedIndexChanging(sender, eventArgs)
    {
        if (!eventArgs._item)
        {
            eventArgs.set_cancel(true);
        }
    }


    This worked great, but later I found that when the control lost focus, it still raised the OnSelectedIndexChanged event on the server. So I wrote client code for OnClientBlur to prevent that too.  However, it didn't work 100%, so I gave up and just handled it completely on the server-side.

    I really believe the RadComboBox should only raise the OnSelectedIndexChanged event on the server when the selected index is actually changed.
  5. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 21 Mar 2011 Link to this post

    Hi Jon,

    You are right that OnSelectedIndexChanged should be fired only when the index is actually changed.
    This problem is due to a bug, which has already been logged.

    Regards,
    Dimitar Terziev
    the Telerik team
Back to Top