Javascript and Telerik Ajax Controls

2 posts, 0 answers
  1. Joe
    Joe avatar
    23 posts
    Member since:
    Nov 2010

    Posted 05 Nov 2012 Link to this post

     

    I have a couple of questions as it pertains to JavaScript and Telerik Ajax controls:

    1 - How can I reference a control using a variable? For example, I'm using the following code to enable/disable a RadTextBox based on a checkbox:

    <script type="text/javascript">
        function enableDisableControl(bEnable) {
            var control = $find("<%= txt_ticketnumber.ClientID %>");
            if (bEnable) {
                control.enable();
            }
            if (!bEnable) {
                control.disable();
            }
        }
    </script>

     

    I would like to make this more modular by doing this:

    <script type="text/javascript">
        function enableDisableControl(bEnable, controlID) {
            var control = $find("<%= controlID.ClientID %>");
            if (bEnable) {
                control.enable();
            }
            if (!bEnable) {
                control.disable();
            }
        }
    </script>

     
    and then calling it from checkbox's onclick client event.

    onclick="enableDisableControl(this.checked, 'txt_ticketnumber');"

    It works with standard ASP controls, but when I attempt to do this with Telerik controls I receive a null reference.

    2 - How can I access ASP Ajax controls from a imported JavaScript. For example, the 1st example listed above works fine if its in the ASPX file wrapped in a RadScriptBlock. But if I move it to its own file (named something.js) and import it with the following code, I can no longer access controls. Its seems "out of scope". I've also tried wrapping it in a RadScriptBlack with no success.

    <script type="text/javascript" src="Scripts/ControlNameLocator.js"></script>


  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Nov 2012 Link to this post

    Hi Joe,

    Try the following code to achieve your scenario.

    C#:
    RadButton1.Attributes.Add("OnClick", "enableDisableControl(false,'" + RadButton2.ClientID.ToString() + "')");

    JS:
    function enableDisableControl(bEnable, controlID)
    {
      var control = $find(controlID);
      control.set_enabled(bEnable);
    }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top