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

Javascript and Telerik Ajax Controls

1 Answer 91 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 2
Joe asked on 05 Nov 2012, 07:44 PM

 

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>


1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 06 Nov 2012, 07:58 AM
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.
Tags
General Discussions
Asked by
Joe
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Share this question
or