在代码中访问 ScriptManager 代理
我有一种情况,我想将“上次修改”时间戳添加到我的 ScriptManager(包含在我的 MasterPage 中)和任何 js 文件(例如“custom.js?2009082020091417”)的路径中ScriptManagerProxies(内容页面)。
我可以轻松地在代码中访问 ScriptManager,然后迭代它的 Scripts 集合以获取我以声明方式设置的脚本路径,然后“设置”一个带有“?[lastmodifiedtimestamp]”的新路径。
问题是,我不知道如何访问可能存在的任何 ScriptManagerProxies。
调试时,我可以在非公共成员(._proxies)中看到代理。我浏览了文档,但看不到您可以在哪里实际公开访问此集合。
我错过了什么吗?
我的内容页的 Page_PreRenderComplete 事件的基类中有以下代码:
ScriptManager sm = ScriptManager.GetCurrent((Page)this);
if(sm != null)
{
foreach (ScriptReference sr in sm.Scripts)
{
string fullpath = Server.MapPath(sr.Path);
sr.PathWithVersion(fullpath); //extension method that sets "new" script path
}
}
上面的代码提供了我在 MasterPage 中定义的一个脚本,但没有提供我在内容页的 ScriptManagerProxy 中定义的其他两个脚本。
I have a situation where I'd like to add a "last modified" timestamp to the paths of my js files (ex. "custom.js?2009082020091417") that are referenced in my ScriptManager (contained in my MasterPage) and in any ScriptManagerProxies (content pages).
I can easily get access to the ScriptManager in code and then iterate through it's Scripts collection to get the Script paths that I've set declaratively and then "set" a new path with the tacked on "?[lastmodifiedtimestamp]".
The problem is, I can't figure out how to get to any ScriptManagerProxies that may exist.
When debugging, I can see the proxies in the non-public members (._proxies). I've looked through the documentation and can't see where you can actually publicly access this collection.
Am I'm missing something?
I have the following code in the base class of my content page's Page_PreRenderComplete event:
ScriptManager sm = ScriptManager.GetCurrent((Page)this);
if(sm != null)
{
foreach (ScriptReference sr in sm.Scripts)
{
string fullpath = Server.MapPath(sr.Path);
sr.PathWithVersion(fullpath); //extension method that sets "new" script path
}
}
The above code gives me the one script I have defined in my MasterPage, but not the two other scripts I have defined in the ScriptManagerProxy of my content page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想出了一个解决办法。似乎唯一可以访问所有合并脚本的地方是主 ScriptManager 的 ResolveScriptReference 事件。在这种情况下,对于每个具有已定义路径的脚本,我使用一个扩展方法,该方法将根据 js 文件的上次修改日期附加一个“版本号”。现在我的 js 文件已“版本化”,当我对 js 文件进行更改时,浏览器将不会缓存旧版本。
母版页代码:
扩展方法:
Came up with a solution. Seems that the only place that all the merged scripts can be accessed is in the main ScriptManager's ResolveScriptReference event. In this event, for each script that has a defined path, I use an extension method that will tack on a "version number" based on the js file's last modified date. Now that my js files are "versioned", when I make a change to a js file, the browser will not cache an older version.
Master Page Code:
Extension Method: