[英]load user control dynamically with ajax AND have jquery document ready functionality
I have a user control that will load via 我有一个将通过加载的用户控件
public static string RenderControl(Control control)
{
var controlWriter = new StringWriter();
var htmlWriter = new Html32TextWriter(tw);
control.RenderControl(writer);
htmlWriter.Close();
return controlWriter.ToString();
}
AJAX used to write the html 用来编写HTML的AJAX
$('#testDiv').html(result.d);
This is called through an ajax Post. 这是通过ajax Post调用的。 It loads the user control fine, but since the javascript document load has already fired I cannot use jquery's document.Ready. 它可以很好地加载用户控件,但是由于已经触发了javascript文档加载,因此我无法使用jquery的document.Ready。
My situation is I need to load a user control dynamically and have jquery document.ready fire , or some equivalent. 我的情况是我需要动态加载用户控件并具有jquery document.ready fire或其他等效项。 I would rather not use an updatepanel but if that is the only means of getting this done then that is what I will use. 我宁愿不使用updatepanel,但是如果那是完成此任务的唯一方法,那将是我将要使用的面板。
What is an elegant solution to my problem? 对我的问题有什么优雅的解决方案?
You can use the built-in jQuery ajaxStop event to fire when an ajax call completes. 您可以使用内置的jQuery ajaxStop事件在ajax调用完成时触发。
http://api.jquery.com/ajaxStop/ http://api.jquery.com/ajaxStop/
I solved something similar with a custom event that fires after the ajax contents were loaded and displayed. 我用自定义事件解决了类似的问题,该事件在加载和显示ajax内容后触发。
Trigger the event inside the ajax function, after load and display: 加载并显示后,触发ajax函数内的事件:
$(document).trigger('ajaxLoadCallback');
And catch it in your document.ready script: 并将其捕获到document.ready脚本中:
$(document).on('ajaxLoadCallback', function() {
// Your ready functions
}
If you create a function for everything that should be executed after document.ready than you can call this function (eg readyFunctions()
) on document.ready and after the firing custom event. 如果为在document.ready之后应执行的所有操作创建函数,则可以在document.ready以及触发自定义事件之后调用此函数(例如readyFunctions()
)。
public partial class Default : Page
{
[WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
}
$(function(){
$.ajax({
type: "POST",
url: "Default.aspx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Do something interesting here.
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.