$(document).ready in ascx page after ajax callback
Asked Answered
P

3

5

I'm having a little problem with this setup here I have a list of .ascx files and they all do different tasks in terms of calculations to the controller itself. So on my .aspx page I click on an Ajax.ActionLink() and this will render that specific .ascx file based on the item I clicked. Within that .ascx are 1-3 events that will fire 2 of them are onclick events and 1 is onload. The onclick event(s) are easier to work with in terms of I can hardcode it directly in the controls event like so onclick="$("#toggleMe3").slideToggle("slow");" and the onload must run when the .ascx is loaded i was testing this in a $(document).ready(function(){}); call, this works fine in the .aspx page but as soon as I try adding it into the .aspx page it doesn't load and its ideal that this works but I have no idea why not. In fact nothing in the script tags work if I insert directly into the .ascx page they only work if hardcoded into the control's events, well some of them at least; the onload and onprerender don't fire.

Pangermanism answered 12/5, 2009 at 17:39 Comment(0)
D
1

I had the same problem, after partial postback script specified in $(document).ready was not executed. I found solution here MSDN - PageRequestManager Class

Looks like adding a script like below fixes the issue

<script type="text/javascript">
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(myReadyFunction);
</script>
Disinter answered 29/3, 2011 at 16:54 Comment(0)
H
1

I've had success using $(document).ready in my partials that get loaded via XHR. Are the views that you're loading via XHR throwing JavaScript exceptions? Or do they contain malformed HTML?

I typically have my $(document).ready method at the bottom of my partial that I load via Ajax, like...

<script type="text/javascript">
$(document).ready(function(){ callMyFunction(); });
</script>
Habit answered 12/5, 2009 at 18:53 Comment(2)
Unfortunately this doesn't work for me, I added the script tags to the bottom still no luck. I'm using MicrosoftAjax.js to control the Ajax calls and not the jquery ajax function.Pangermanism
This works for me, at least in FF3.5 and IE7 on Windows XP. However I do not understand why, as the jQuery documention doesn't handle this case...Apocarpous
L
1

I had a hard time understanding your question...but here it goes.

If you are loading date using AJAX calls, the $(document).ready() event will not fire -- because the page was already loaded. You are just loading more data now.

If you already know the controls what will apear, pre-load the JavaScript, but instead of just binding using the click event handler, use the live handler.

so

$("#myControl").click(....);

turns into

 $("#myControl").live("click", ....);

Sorry if this isn't what you were looking for.

Linneman answered 12/5, 2009 at 19:13 Comment(1)
if the page is not dynamically loaded, yes. Otherwise you need the live to preload the event handlers from the aspx. Otherwise you will have to add some more hooks on the client side to know when the content is loaded, and the call the loaded javascript.Linneman
D
1

I had the same problem, after partial postback script specified in $(document).ready was not executed. I found solution here MSDN - PageRequestManager Class

Looks like adding a script like below fixes the issue

<script type="text/javascript">
  Sys.WebForms.PageRequestManager.getInstance().add_endRequest(myReadyFunction);
</script>
Disinter answered 29/3, 2011 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.