1

In OnClientKeyPressing event of RadComboBox, I am checking if entered text inside RadComboBox is greater than 5 characters, then Enable the asp:Button and if not then Disable the asp:Button.

Both RadComboBox and asp:Button are inside RadGrid.

This works perfetcly if I type in 5 or 5+ characters manually, but not if i PASTE 5 or 5+ characters inside RadComboBox, if I paste then asp:Button remains disable.

Reason is: when I paste something in RadComboBox, OnClientKeyPressing event raise and since the key(ctrl+v) is pressed only one time, so the Length of text is always 0 and as per condition (If length < 3, Disable button) it remains Disable.
Please note that the length starts from 0 not from 1.

I know the reason but I dont know how to make it detect whole text-length after I paste anything in RadComboBox

Below is the Javascript till now:

<telerik:RadCodeBlock ID="rcb" runat="server">
        <script type="text/javascript">
            function HandleKeyPress(sender, eventArgs) {

                var len = sender.get_text().length; 

                var comboID = sender.get_id(); 
                var btnSearchID = comboID.replace("ddlAccountCode", "btnSearch");
                var btnCtrl = document.getElementById(btnSearchID);                    

                //code to chk Ctrl + V               
                var e = eventArgs.get_domEvent(); 
                if (e.keyCode == 86 && e.ctrlKey) 
                {
                    var len2 = sender.get_text().length; //-------Issue here : pasted text/length is always 0 (as the key is pressed only one time)
                    alert(len2);

                    if (len2 > 4) {
                        //alert("ctrl+v : Enable");
                        btnCtrl.disabled = false; //Enable
                    }
                    else {
                        //alert("ctrl+v : Disable");
                        btnCtrl.disabled = true; //Disable
                    }
                }
                else if (eventArgs.get_domEvent().keyCode != 8 && len > 4)
                {
                    //alert("Chk manually entered text");
                    btnCtrl.disabled = false; //Enable
                }
                else if (eventArgs.get_domEvent().keyCode == 8 && len > 4){   
                    //alert("Backspace Enable");
                    btnCtrl.disabled = false; //Enable 
                }
                else {
                    btnCtrl.disabled = true; //Disable
                }
            }
        </script>
    </telerik:RadCodeBlock>

HTML:

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource">
                            <MasterTableView>
                                <Columns>
                                    <telerik:GridTemplateColumn HeaderText="Acc">
                                        <EditItemTemplate>                                                
                                            <telerik:RadComboBox ID="ddlAccountCode" runat="server" Height="200" Width="260"
                                             AllowCustomText="true" OnClientKeyPressing="HandleKeyPress" >
                                                <Items>
                                                    <telerik:RadComboBoxItem Text="Gg" />
                                                    <telerik:RadComboBoxItem Text="Gg1" />
                                                    <telerik:RadComboBoxItem Text="Gg3" />
                                                </Items>
                                            </telerik:RadComboBox>
                                            <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" OnClientClick="ButtonClicked()" UseSubmitBehavior="true" />                                      
                                        </EditItemTemplate>
                                    </telerik:GridTemplateColumn>
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>
timz_123
  • 435
  • 1
  • 9
  • 47

1 Answers1

0

The OnClientKeyPressing event is actually triggered between the time when the key is pressed and when the control is populated with the text from the key press. This means it will always show your text's length - 1 in normal cases where the user is typing, and will have no text yet when the user pastes into it.

I suggest using the OnClientTextChange event instead, which will trigger after the text in the control has actually changed. In that case you shouldn't need to write a conditional statement to handle ctrl+v input. However, this will only trigger after the combo box has lost focus.

Another solution, if you need it to trigger when the user presses the key and not after the combo box loses focus, would be to wrap your code inside a setTimeout call with 1 ms for the second parameter, which would allow the control to have its text updated before it was checked against your code.

Will P.
  • 8,437
  • 3
  • 36
  • 45
  • Thank you for the reply. Understood that. I just want to know that in my above Javascript code, is there anyway to get the length of text AFTER I paste the characters inside `RadComboBox` ? Also, I tried to use `setTimeout` but then my `OnClientKeyPressing` event is not working under it. Please reply – timz_123 Sep 30 '15 at 06:09
  • Hi, so I copied your function into a fiddle and added the setTimeout call as I would expect it to work. This is untested on my end but see if it works for you: http://jsfiddle.net/avzn19p4/. Note, this is copied directly and doesn't account for the fact that the `sender.get_text().length` value should actually reflect the full length now, and not the length - 1, so assuming this works you'll want to make that modification to the code. – Will P. Sep 30 '15 at 17:07
  • Thank you so very much for the help. It works like charm! I implemented the `setTimeout` call method suggested by you (referred your fiddle as well and modified my Javascript again -updated in posted question). All working fine now :) – timz_123 Oct 01 '15 at 07:26