简体   繁体   English

如何在ASP.NET MVC项目中模拟ScriptManager.RegisterStartupScript()

[英]How to emulate ScriptManager.RegisterStartupScript() in ASP.NET MVC project

I am using ASP.NET MVC project and jQuery to handle async requests. 我正在使用ASP.NET MVC项目和jQuery来处理异步请求。 Sometimes for an async request I need an initialization JavaScript snippet to be returned and executed together with an HTML snippet (I want this JavaScript to be rendered on server). 有时,对于异步请求,我需要返回一个初始化JavaScript代码段并与HTML代码段一起执行(我希望在服务器上呈现此JavaScript)。

For now I am using my own ScriptsHelper class with a RegisterStartupScript() method (adds data to HttpContext.Current.Items). 现在我使用自己的ScriptsHelper类和RegisterStartupScript()方法(将数据添加到HttpContext.Current.Items)。 Then in global.asax HttpApplication.EndRequest() for async requests I append all registered scripts to the output (enclosed in tags). 然后在异步请求的global.asax HttpApplication.EndRequest()中,我将所有已注册的脚本附加到输出(包含在标记中)。 jQuery.fn.load() successfully executes them when the received HTML is appended to the DOM - this is exactly what I need. 当收到的HTML附加到DOM时,jQuery.fn.load()成功执行它们 - 这正是我需要的。 Do you think it is "correct" (good) solution, or maybe you can suggest something better? 你认为这是“正确的”(好)解决方案,还是你可以提出更好的建议? Thanks. 谢谢。

I encountered a similar situation in a project before I started using jquery and wound up resorting to using eval() . 在我开始使用jquery之前,我在一个项目中遇到了类似的情况,并且使用了eval() I was returning a javascript text snippet as part of the value of a non-visible DOM element along with the rest of the HTML I was inserting into the DOM. 我正在返回一个javascript文本片段作为不可见DOM元素的值的一部分以及我插入DOM的其余HTML。

I had a lot of trouble debugging this type of dynamically generated javascript in IE6 so this solution worked out decently for me because I could inspect the value of the DOM element that was getting eval'd. 我在IE6中调试这种动态生成的javascript时遇到了很多麻烦,所以这个解决方案适合我,因为我可以检查得到eval'd的DOM元素的值。 However, I've always been leery of using eval . 但是,我一直对使用eval持怀疑态度。

It sounds like your solution has a reasonable architecture for the server-side rendering as opposed to my spaghetti-code and this technique looks like it fits right in with the spirit of jQuery.fn.load so I wouldn't worry too much. 听起来你的解决方案有一个合理的服务器端渲染架构,而不是我的意大利面条代码,这种技术看起来很适合jQuery.fn.load的精神,所以我不会太担心。

Yeah I think that jQuery is calling eval() internally for you - are you using append? 是的,我认为jQuery在内部为你调用eval() - 你使用追加吗? I think that this technique works pretty good most of the time. 我认为这种技术大多数时候效果都很好。 If the data returned from the server is in a list that is repeated you may want to look at using the live function added to jQuery 1.3 It allows you to attach an event to all current and future elements matching your selector. 如果从服务器返回的数据位于重复的列表中,您可能希望查看使用添加到jQuery 1.3的实时函数。它允许您将事件附加到与您的选择器匹配的所有当前和未来元素。 Checkout these articles: 查看这些文章:

Also as something else to consider is a tool to help manage scripts and css for asp.net mvc . 另外还有一些需要考虑的工具来帮助管理asp.net mvc的脚本和CSS I have been using this and I love it as it combines files as well as compresses them. 我一直在使用它,我喜欢它,因为它结合文件以及压缩它们。 You can configure it all from you web config too. 您也可以从Web配置中对其进行全部配置。

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

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