Bad timing issue, Telerik RadEditor control's client side events and jquery's on page ready event
I have an object that I initialize, and I pass in a parameter that is a radeditor control.
I think set some client side events of the radeditor using my object like this:
<telerik:RadEditor ...
OnClientLoad="My_Object.Som开发者_高级运维eFunction" ... />
The problem is, my object isn't wired up correctly unless I put it in jquery's ready function, but that seems to fire AFTER the RadEditor event, and as a result I get the error message:
My_Object is undefined
I am doing this:
<script type="text/javascript">
$(document).ready(function () {
editor = $find("<%=RadEditor1.ClientID %>");
My_Object = new MYOBJECT(editor);
});
</script>
The call to $find("<%=RadEditor1.ClientID %>");
is always null if I try and set it outside of ready function.
But when I put it inside the ready function, My_Object
is being instantiated too late and when I set the client side events on the RadEditor control it says the method is null.
How can I solve this timing issue?
I have had this same issue with telerik controls as well. You need to put the logic you have in your $(doucment).ready into a client side page_load instead. $(document).ready happens before the ASP.Net client side page_load life cycle has happened so the telerik controls won't be available yet client side.
function pageLoad(sender, e)
{
editor = $find("<%=RadEditor1.ClientID %>");
My_Object = new MYOBJECT(editor);
}
This method will be called every time the ASP.Net page goes through a full life cycle via full page load or partial postback.
You can also check to see if a partial postback is currently happening before the running the logic if you only want this event to happen on the initial page load but not inside any partial loads.
function pageLoad(sender, e)
{
if (!e.get_isPartialLoad())
{
editor = $find("<%=RadEditor1.ClientID %>");
My_Object = new MYOBJECT(editor);
}
}
You could put the script block that initializes your object at the very end of the <body>
. You'd not need to rely on the "ready()" handler, but you'd also ensure that the DOM element for your editor were ready to be found and manipulated. (That's why it's not working, probably, when you don't use the "ready" handler in a script in the <head>
— the DOM doesn't exist when it runs.)
the $(document).ready() will fire before the editor is initialized. The editor will be available after the init event of the ASP.NET AJAX client framework fires. This happens after the jQuery .ready() code. You should create the My_Object without using a dependence on the editor object, because you will get a reference from the handler of the OnClientLoad function anyway:
<script type="text/javascript">
$(document).ready(function () {
My_Object = new MYOBJECT();
});
</script>
the SomeFunction() code will have a reference to the editor passed as a parameter:
MYOBJECT.prototype.SomeFunction = function(editor){
//editor is a reference to the RadEditor client-side object
}
精彩评论