Telerik MVC controls, OnDocumentReady is firing twice


Telerik MVC controls, OnDocumentReady is firing twice



I'm new to Telerik MVC controls, so this may be something simple.



I started out with making a new TelerikMvcApplication solution.



Site.Master:


</div>
<%: Html.Telerik().ScriptRegistrar().DefaultGroup(group =>
group.Combined(true).Compress(true).Add("Index.js")) %>
</body>
</html>



Index.aspx:


<%: Html.Telerik().ScriptRegistrar().OnDocumentReady("Index.Init()") %>
</asp:Content>



Index.js:


var Index = function () {
return {
Init: function () {
alert('1');
}
};
} ();



When I load Index.aspx, the Init() function in Index.js is fired twice, so I see 2 alerts one after another. If I assign a click handler to a button inside the Init() function and hit the button with FireBug, it is actually showing 2 events being assigned to the button. Why is this happening?



After some more firebugging, I figured out that both calls to ScriptRegistrar are being rendered. How do I make only master one to render?




2 Answers
2



To answer my own question, the problem was in the use of


<%: %>



with colon.



To fix it, I removed colon character from both Master and View, and called Render() in Master.



Master:


<% Html.Telerik().ScriptRegistrar().DefaultGroup(group =>
group.Combined(true).Compress(true).Add("json2.js")).Render(); %>



View:


<% Html.Telerik().ScriptRegistrar().Scripts(scripts =>
scripts.AddGroup("IndexGroup", group =>
group.Add("~/Scripts/Index.js"))).OnDocumentReady("Index.Init()"); %>



For MVC Razor ViewEngine:


@{
Html.Telerik().ScriptRegistrar().OnDocumentReady(
@<text>
alert('onLoaded');
</text>);
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

List of Kim Possible characters

Audio Livestreaming with Python & Flask

NSwag: Generate C# Client from multiple Versions of an API