How to stop postback when enter key is pressed

1 posts, 0 answers
  1. Telerik Admin
    Telerik Admin avatar
    1679 posts
    Member since:
    Oct 2004

    Posted 14 Jun 2007 Link to this post

    Requirements

    To convert code
    from posted project(s)
    Telerik online converter
    RadControl version

    RadComboBox v2.7.1 or Telerik.Web.UI v2008.1.415.20

    .NET version

     2.x

    Visual Studio version

    2005

    programming language

    VB

    browser support

    all browsers supported by RadComboBox


    PROJECT DESCRIPTION
    This project illustrates how to stop postback after pressing the enter key.

    1. Detect that Enter using the OnKeyPressing event of RadComboBox;

    2. Return false in the OnClientSelectedIndexChanging event handler of RadComboBox if Enter has been pressed



    RadComboBox Declaration
                <telerik:RadComboBox  
                    ID="RadComboBox1"  
                    runat="server"  
                    OnClientKeyPressing="DetectEnter"  
                    OnClientSelectedIndexChanging="CheckEnter"   
                    AutoPostBack="True"  
                    Width="150px"
                    <Items> 
                        <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem1" /> 
                        <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem2" /> 
                        <telerik:RadComboBoxItem runat="server" Text="RadComboBoxItem3" /> 
                    </Items> 
                </telerik:RadComboBox> 

    JavaScript
        <script type="text/javascript"
            var ent = false
             
            function DetectEnter(sender, eventArgs) 
            { 
                var keyCode = eventArgs._domEvent.keyCode; 
                 
                if (keyCode == 13) 
                { 
                   ent = true
                } 
                else 
                { 
                   ent = false
                } 
            } 
     
            function CheckEnter(sender, eventArgs) 
            { 
                var combo = $find("<%= RadComboBox1.ClientID %>"); 
                 
                combo.hideDropDown(); 
                 
                var temp = ent; 
                 
                ent = false
                 
                eventArgs.set_cancel(temp); 
            } 
        </script> 




    RadComboBox Declaration
            <radC:RadComboBox  
                ID="RadComboBox1"  
                runat="server"  
                OnClientKeyPressing="DetectEnter"  
                OnClientSelectedIndexChanging="CheckEnter" 
                AutoPostBack="True"  
                SkinsPath="~/RadControls/ComboBox/Skins"  
                Width="150px"
                <Items> 
                    <radC:RadComboBoxItem ID="RadComboBoxItem1" runat="server" Text="RadComboBoxItem1" /> 
                    <radC:RadComboBoxItem ID="RadComboBoxItem2" runat="server" Text="RadComboBoxItem2" /> 
                    <radC:RadComboBoxItem ID="RadComboBoxItem3" runat="server" Text="RadComboBoxItem3" /> 
                </Items> 
            </radC:RadComboBox> 

    JavaScript
        <script type="text/javascript"
            var ent = true
     
            function DetectEnter(combo, eventArgs) 
            { 
                var keyCode = eventArgs.keyCode; 
     
                if (keyCode == 13) 
                { 
                   ent = false
                } 
                else 
                { 
                  ent = true
                } 
            } 
     
            function CheckEnter() 
            { 
                var combo = <%= RadComboBox1.ClientID %>; 
                 
                combo.HideDropDown(); 
                 
                var temp = ent; 
                 
                ent = true
                 
                return temp; 
            } 
        </script> 
Back to Top