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

$find doesn't work when called through ScriptManager.RegisterStartupScript

2 Answers 325 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Piotr
Top achievements
Rank 1
Piotr asked on 04 Aug 2010, 11:07 AM

Hi everyone

First of all, I'm aware of another existing thread that has the almost same title; I've read it, and it didn't solve my problem; therefore I decided to write a new thread.

My problem goes as follows:

I have an UpdatePanel placed in a page, which is defined as follows:

1.<asp:UpdatePanel ID="up" runat="server" Visible="false"
2. <ContentTemplate
3.  <asp:Label runat="server" Visible="false" ID="myLabel"></asp:Label
4. </ContentTemplate
5.</asp:UpdatePanel>

I need the Label and the Label's type to invoke some JavaScript with the ScriptManager.RegisterStartupScript method, which we will see later; both the UpdatePanel and Label are only needed for this purpose, and the RadComboBox is not placed in there!

The following is the JavaScript method I need to invoke using the ScriptManager.RegisterStartupScript:

1.function MyFunc(){
2. var combo = $find("MyRadComboBox.ClientID");
3. combo.get_text();
4.}

And the following is the RegisterStartupScript method I use in the code-behind file:

1.ScriptManager.RegisterStartupScript(myLabel, myLabel.GetType(), "MyKey", "MyFunc();", true);

Everytime the JavaScript method gets invoked by the RegisterStartupScript method from the code-behind, I get the following error on the client-side:

Uncaught TypeError: Cannot call method 'get_text' of null

The RadComboBox can't be found on the client-side using $find in this special situation. If I try to access it from Chrome's JS Console using the $find method, I'll get the object, and the invocation of a method on it works perfectly. 

What am I doing wrong here? How can I fix this? It's a pretty annoying little problem, and I hope somebody can point me to the right direction.

Thanks very much in advance guys!

2 Answers, 1 is accepted

Sort by
0
Simon
Telerik team
answered on 05 Aug 2010, 03:29 PM
Hello Piotr,

The error is caused by the fact that the RadComboBox' client-side object is still not initialized when the function executes.

One way of resolving this would be to call it with a small timeout, e.g.
ScriptManager.RegisterStartupScript(myLabel, myLabel.GetType(), "MyKey", "setTimeout(MyFunc, 100);", true);

Best wishes,
Simon
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jakub
Top achievements
Rank 1
answered on 14 Apr 2012, 02:19 PM
Hi. Actually the timeout can be zero for partial postbacks. Unfortunately it is not realiable at all for normal postbacks in Chrome. Client-side load event could be used as universal solution to these startup script issues. Load event has its own nuisances. They leads to something like this:
(function () {
    var fn = function () {
        var combo = $find("MyRadComboBox.ClientID")
        combo.get_text();
    };

    Sys.Application.add_load(fn);
})();

The script is executed properly after partial and full postback.

More info in this blog post.
Tags
ComboBox
Asked by
Piotr
Top achievements
Rank 1
Answers by
Simon
Telerik team
Jakub
Top achievements
Rank 1
Share this question
or