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

Problem with Javascript and $find

1 Answer 159 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Henrik Tegman
Top achievements
Rank 1
Henrik Tegman asked on 11 Mar 2011, 02:31 PM
Hello, I'm not sure where to post this but I'll put it here.

I have a project that has a simple aspx and some ascx.
In these ascx I want to use javascript, at first I simply put the javascript right into the ascx with the script tags.
When doing this I could easy use $find("<%= myControl.ClientID %>") to find the control and use the various clientside methods that the diffrent radcontrols come with.

Now as the project has grown and so has my javascripts I decided to move them to a separate jsfile. And this is where my problem started.
I have a RadScriptManager in my aspx and the aspx has the ascx, but when I try to use $find("<%= myControl.ClientID %>") I get null back.

BUT if I use $find("myControl") then I can find it, the problem with that is that I have to find the genereated name, and If I place my ascx in diffrent parts in the aspx I would have to change this every time.

Why doesn't it work after I moved it to its own js file?

I guess I could move it back, but it's so much better and cleaner to have it in its own js file, that way I can also debugg it and see what happends since I can't do that if I have it in the ascx script tag.

Any help in this matter would be very welcome, I'm guessing I'm missing something here, something that I don't understand.

Thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 14 Mar 2011, 11:10 AM
Hello Henrik,

Javascript external files are not run through the ASP.NET engine, so the server tag <%=...%> is never executed in that case. In order to access any control ClientID, use a global variable in the ascx file itself, and save the ClientID when page loads.  Now you will be able to access the control (from external JS file) uisng the ClientID saved in global variable.

ascx:
<script type="text/javascript">
    var ID = '<%= RadTextBox1.ClientID %>';
</script>

External JS:
function getGrid()
 {
    var TextBox1= $find(ID);
 }

Thanks,
Shinu.
Tags
General Discussions
Asked by
Henrik Tegman
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or