Getting a ScriptReference from a ScriptResourceMapping definition in a custom control

stephanei3

New Member
I am building a custom control with client side scripts that I would like to reference using \[code\]ScriptManager.ScriptResourceMapping\[/code\] (to make use of the \[code\]Path\[/code\] and \[code\]DebugPath\[/code\] attributes).I would like the custom control to be easily ported to other projects - i.e. I would like to drag and drop the codebehind files (and eventually make the control a separate DLL, but for now the drag and drop will suffice). I would therefore like to avoid (1) having the client script as an embedded resource, (2) referenced as a \[code\]WebResource\[/code\] in the \[code\]AssemblyInfo\[/code\], or (3) have the \[code\]ScriptManager.ScriptResourceMapping.AddDefinition\[/code\] in \[code\]global.asax\[/code\]. In simple terms can I get the script management code to be in just the custom control's code?At the moment I am getting an error stating that the script reference cannot be found in the assembly, and I guess I am setting the wrong assembly.My custom control code is as follows:\[code\]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 FunctionEnd Class\[/code\]I hope the question makes sense. Thanks for taking the time to read through.Ali
 
Back
Top