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

External JS file cannot detect telerik items

1 Answer 241 Views
ScriptManager and StyleSheetManager
This is a migrated thread and some comments may be shown as answers.
Hossam
Top achievements
Rank 1
Hossam asked on 24 Jan 2014, 08:30 AM
Hello
I try to work with telerik button using client side in external JS file but i cannot detect it, but when i put the code inside the page it works good.
this is Page HTML code 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
<!DOCTYPE html>
 
<head runat="server">
    <title></title>
    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
        <script type="text/javascript" src="JavaScript.js"></script>
    </telerik:RadScriptBlock>
</head>
<body>
    <form id="form1" runat="server">
         
    <div>
        <telerik:RadButton ID="RadButton1" runat="server" Text="RadButton" OnClientClicked="test"></telerik:RadButton>
    </div>
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
 
    </form>
     
</body>
</html>
and this is my JS file code
function test(sender,args) {
    var button = $find("<%= RadButton1.ClientID %>");
    alert(button.get_text());
}
thanx in advance

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 27 Jan 2014, 02:41 PM
Hi Hossam,

Server code blocks (<%= ..... %>) can only be used in compiled files (aspx, ascx) and not in external files that are not parsed by the ASP engine. Perhaps this blog post can show you one way of working with control references: http://blogs.telerik.com/jefffritz/posts/13-01-21/simplify-javascript-control-references-in-asp.net-webforms.

On a side note - the client-side events our controls raise pass the control instance as a first argument, so you can use that instead:
function test(sender,args) {
    alert(sender.get_text());
}


Regards,
Marin Bratanov
Telerik
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 UI for ASP.NET AJAX, subscribe to the blog feed now.
Tags
ScriptManager and StyleSheetManager
Asked by
Hossam
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or