【问题标题】:Getting a ScriptReference from a ScriptResourceMapping definition in a custom control从自定义控件中的 ScriptResourceMapping 定义中获取 ScriptReference
【发布时间】: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


    【解决方案1】:

    我自己回答了这个问题,我对 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多