2012-12-14 47 views
1

我正在使用ScriptManager.ScriptResourceMapping(使用PathDebugPath属性)构建一个客户端脚本的客户端脚本的自定义控件。我想自定义控件很容易移植到其他项目 - 即我想拖放代码隐藏文件(并最终使控件为单独的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 

我希望这个问题是有道理的。感谢您花时间阅读。

阿里

回答

2

这个回答我自己,我是越来越糊涂与组件和ScriptReference构造函数。我只想要一个带有(映射)名称的ScriptReference,所以我使用空白构造函数,然后设置Name。然后我可以删除程序集信息。

调整以下排序的问题出来了:

Public Sub New() 

    ' Add the resource mapping 
    ScriptManager.ScriptResourceMapping.AddDefinition("MyControlScript", New ScriptResourceDefinition With { 
     .Path = "Path.To.MyControlScript.minimised.js", 
     .DebugPath = "Path.To.MyControlScript.original.js" 
    }) 

    ' Set the script reference 
    _scriptReference = New ScriptReference() With {.Name="MyControlScript"} 

End Sub 
相关问题