我正在使用ScriptManager.ScriptResourceMapping
(使用Path
和DebugPath
属性)构建一个客户端脚本的客户端脚本的自定义控件。我想自定义控件很容易移植到其他项目 - 即我想拖放代码隐藏文件(并最终使控件为单独的DLL,但现在拖放就足够了)。因此,我想避免(1)将客户端脚本作为嵌入式资源,(2)在AssemblyInfo
中引用为WebResource
,或者(3)在global.asax
中具有ScriptManager.ScriptResourceMapping.AddDefinition
。从自定义控件中的ScriptResourceMapping定义中获取ScriptReference
简而言之,我可以将脚本管理代码放在自定义控件的代码中吗?
此刻我收到一个错误,指出脚本引用无法在程序集中找到,我想我设置了错误的程序集。
我的自定义控件的代码如下:
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
我希望这个问题是有道理的。感谢您花时间阅读。
阿里