This is a migrated thread and some comments may be shown as answers.

Enter Key Press When No Item Selected

3 Answers 163 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Jon Shipman
Top achievements
Rank 1
Jon Shipman asked on 06 Mar 2011, 11:18 PM
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?

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 08 Mar 2011, 12:24 PM
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.
0
Jon Shipman
Top achievements
Rank 1
answered on 15 Mar 2011, 10:03 PM
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.
0
Dimitar Terziev
Telerik team
answered on 21 Mar 2011, 05:10 PM
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
Tags
ComboBox
Asked by
Jon Shipman
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Jon Shipman
Top achievements
Rank 1
Dimitar Terziev
Telerik team
Share this question
or