有效使用ASP.NET RegisterStartupScript方法 [英] Efficient Use of the ASP.NET RegisterStartupScript method

查看:84
本文介绍了有效使用ASP.NET RegisterStartupScript方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在后台代码中加载数据时,我经常发现我想基于我的数据运行多个javascript函数.使用StringBuilder编译所有函数调用并一次发出RegisterStartupScript效率更高,或者如果每次需要时都发出RegisterStartupScript的性能是一样的吗?

When I load data in my code-behind, I find that often I want to run several javascript functions based on my data. It it more efficient to use a StringBuilder to compile all of my function calls and issue RegisterStartupScript one time, or is performance the same if I issue RegisterStartupScript everytime I need it?

推荐答案

我认为您使用 StringBuilder 的实现会更有效率,因为您无需创建任何控件,而仅注册完成后编写脚本.您可能希望继续使用 StringBuilder 方法的另一个原因是,已注册脚本的顺序是否重要.

I would think that your implementation with a StringBuilder would be a bit more efficient as you are not creating any controls and only register the script once you are finished. The other reason that you may want to stay with your StringBuilder approach is if the order of the registered scripts is important.

来自 MSDN:

由RegisterStartupScript方法添加的脚本块在页面加载完成时但在引发页面的OnLoad事件之前执行. 不能保证脚本块按注册顺序输出. 如果脚本块的顺序很重要,请使用StringBuilder对象将脚本收集在一起.一个字符串,然后将它们全部注册在一个客户端脚本块中.

The script block added by the RegisterStartupScript method executes when the page finishes loading but before the page's OnLoad event is raised. The script blocks are not guaranteed to be output in the order they are registered. If the order of the script blocks is important, use a StringBuilder object to gather the scripts together in a single string, and then register them all in a single client script block.

这篇关于有效使用ASP.NET RegisterStartupScript方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆