简体   繁体   English

jQuery和ASP.NET:在母版页完成初始化后执行站点代码

[英]jQuery and ASP.NET: Execute site code after master page has finished initialization

I'm having a master page in ASP.Net, which downloads various files via AJAX and does some initialization work for a library, that I'm using, after that. 我在ASP.Net中拥有一个母版页,此页可通过AJAX下载各种文件,然后对我正在使用的库进行一些初始化工作。 I'm using jQuery's $.when().then syntax for that. 我正在使用jQuery的$ .when()。then语法。

My individual page requires all those settings to be made before executing it's code. 我的个人页面要求在执行代码之前进行所有这些设置。 Right now, I'm using the $(function () {}) syntax on my page. 现在,我在页面上使用$(function(){})语法。 The problem with this is, that it executes right after the page has rendered, but long before my when.then construct has finished. 问题是,它在页面渲染之后立即执行,但是在我的when.then构造完成之前很久才执行。

Is there way to wait for that construct? 有没有办法等待那个构造? Can I somehow fire a custom event from my master page code after initialization, and attach my content page function to that? 初始化后,能否以某种方式从我的母版页代码中触发自定义事件,并将内容页面功能附加到该事件? Or are there even better solutions? 还是有更好的解决方案?

PS: To make it clear, we're talking about client-side-only code here! PS:为了清楚起见,我们在这里谈论的是仅客户端代码!

Do not wrap in the document ready $(function () {}) but instead in a custom event. 不要在文档中准备好$(function () {}) ,而是在自定义事件中包装。

$(document).on('mycustom',function () {});

THEN trigger that in the then 然后在then触发

$.when().then(something).then(function(){$(document).trigger("mycustom");});

In the master page you can set the when/then chain to a variable and continue to chain it in the detail page. 在母版页中,您可以将when / then链设置为变量,然后继续在详细信息页中将其链接。 They are, after all, one page client-side. 毕竟,它们是客户端的一页。 So as long as the initial chain comes first, you can append to it all you like. 因此,只要初始链位于第一位,您就可以随便添加所有内容。

So you might set the first chain to a variable: 因此,您可以将第一个链设置为变量:

var eventChain = $.when(/.../).then(/.../);

Then in the detail page: 然后在详细信息页面中:

eventChain.then(/.../);

So the detail page is really just picking up where the master page left off. 因此,详细信息页面实际上只是从母版页停止的地方开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM