RadInput + ClientEvents.OnKeyPress + MaxLength

2 posts, 0 answers
  1. Chris T.
    Chris T. avatar
    31 posts
    Member since:
    Apr 2008

    Posted 16 Dec 2011 Link to this post

    This must be my week for strange problems.  Actually, I'm upgrading half a million lines of code from ASP.NET 2.0 to 4.0, and taking my telerik from 2009 Q3 to 2011 Q3, so I've got to catch up on 2 - 5 years of advancements.

    Anyway, I'm seeing some strageness in RadInput.  I'm trying to add a specific default button for the enter key. Simple enough, right?  Handle the keypress and act on keyCode 13.

    I've found through testing though, that when you have MaxLength defined on the field, characters don't seem to be accepted.  The enter key still works fine, but nothing shows up in the field.

    Here's what I did:
    s = string.Empty;
    s += "function " + this.ClientID + "_defaultEnter(sender, eventArgs) { \r\n";
    s += "  var c = eventArgs.get_keyCode(); \r\n";
    s += "  var btn = document.getElementById('" + btnSearch.ClientID + "'); \r\n";
    s += "  if (c == 13){ \r\n";
    s += "    btn.click(); \r\n";
    s += "    eventArgs.set_cancel(true); \r\n";
    s += "  } \r\n";
    s += "}\r\n";
    this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.ClientID + "_defaultEnter", s, true);
    fldCaseNumber.ClientEvents.OnKeyPress = this.ClientID + "_defaultEnter";

    And yes, I know (now) that strings are immutable and I should be using a StringBuilder instead of += ... let's stick to the topic, eh? :)

    Anyway, I can live without the MaxLength, I just wanted to share the pain and maybe help someone who can't figure out why their keyboard suddenly stopped working on that textbox.

  2. Vasil
    Vasil avatar
    1642 posts

    Posted 16 Dec 2011 Link to this post

    Hello Chris,

    Thank you for sharing your solution with the community.

    I would make just a small comment (not sure if it will make a difference anyway), first cancel the event and then click the button.

    All the best,
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Back to Top