【发布时间】:2012-12-14 12:33:54
【问题描述】:
我正在使用客户端脚本构建一个自定义控件,我想使用 ScriptManager.ScriptResourceMapping 引用它(以利用 Path 和 DebugPath 属性)。
我希望自定义控件能够轻松移植到其他项目 - 即我想拖放代码隐藏文件(并最终使控件成为单独的 DLL,但现在拖放就足够了)。因此,我想避免 (1) 将客户端脚本作为嵌入式资源,(2) 在 AssemblyInfo 中引用为 WebResource,或 (3) 在 global.asax 中使用 ScriptManager.ScriptResourceMapping.AddDefinition。
简单来说,我可以让脚本管理代码只在自定义控件的代码中吗?
目前我收到一条错误消息,指出在程序集中找不到脚本引用,我想我设置了错误的程序集。
我的自定义控件代码如下:
Public Class MyControl
Inherits System.Web.UI.LiteralControl
Implements ISectionControl, IScriptControl
Private _scriptReference As ScriptReference
Public Sub New()
' Add the resource mapping
ScriptManager.ScriptResourceMapping.AddDefinition("MyControlScript", New ScriptResourceDefinition With {
.ResourceAssembly = System.Reflection.Assembly.GetExecutingAssembly,
.ResourceName = "MyControlScript.js",
.Path = "Path.To.MyControlScript.minimised.js",
.DebugPath = "Path.To.MyControlScript.original.js"
})
' Set the script reference
_scriptReference = New ScriptReference("MyControlScript.js", Assembly.GetExecutingAssembly.FullName)
End Sub
Protected Overrides Sub OnPreRender(e As System.EventArgs)
MyBase.OnPreRender(e)
' Register the script
ScriptManager.GetCurrent(Page).RegisterScriptControl(Of MyControl)(Me)
' Some code to set the Text of the literal control
' ...
End Sub
Public Function GetScriptDescriptors() As System.Collections.Generic.IEnumerable(Of System.Web.UI.ScriptDescriptor) Implements System.Web.UI.IScriptControl.GetScriptDescriptors
Return New ScriptDescriptor() {}
End Function
Public Function GetScriptReferences() As System.Collections.Generic.IEnumerable(Of System.Web.UI.ScriptReference) Implements System.Web.UI.IScriptControl.GetScriptReferences
Return New ScriptReference() {_scriptReference}
End Function
End Class
我希望这个问题是有意义的。感谢您花时间阅读。
阿里
【问题讨论】:
标签: asp.net .net vb.net custom-controls