I have some javascript in the <head> section of a master page. I want to move it to a server control project. What is the simplest way to do it? I can create a .js file containing the javascript, then add it ot the server control project as an embedded resource.
The web site has a reference to the server control dll.
I added the .js file to the asp.net server control project as an embedded resource. I added the WebResourceAttribute line to AssemblyInfo.cs in the server control project.
Since the original javascript was in the <head> part of Site.master, I added the following to Site.master.cs in the web site project.
It now works. I changed the type arg to RegisterClientScriptResource. I randomly picked a class in the server control project. What I really want to do is
So based on your further description, actually you will want the script resource's url path at runtime so that you can dynamically bind it or assign it to some properties or markup on the page(from your control's code).
I think Decker's original suggestion have made in the right direction since for ASP.NET custom server control( we do not have the entire control over the Page's rendering and output), we need to leverage the "Page.ClientScript"(the script manager) to register
some script or reference into the page content.
And instead of the RegisterClientscript method, you can use another method "GetWebResourceUrl". This method can help directly return the url path of a certain script(or other static resource file) which is emit from an embeded resource in control's assembly:
BTW, for debugging and tracing such kind of dynamically added script reference, I suggest you use fiddler or the built-in IE develpment toolbar to watch the network trace to see if the dynamic reference is correctly registered on the page(from your server
control's code).
swartzbill20...
Member
202 Points
102 Posts
migrate javascript to library
Sep 07, 2011 04:28 PM|LINK
I have some javascript in the <head> section of a master page. I want to move it to a server control project. What is the simplest way to do it? I can create a .js file containing the javascript, then add it ot the server control project as an embedded resource. The web site has a reference to the server control dll.
Bill
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: migrate javascript to library
Sep 09, 2011 01:14 AM|LINK
Hello,
In Assembly.cs file, please add this:
[assembly: WebResource("CustomControls.GridViewScript.js", "text/javascript")]
And then in your OnInit, please do this:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string resourceName = "CustomControls.GridViewScript.js";
ClientScriptManager cs = this.Page.ClientScript;
cs.RegisterClientScriptResource(typeof(CustomControls.CustomGridView), resourceName);
}
For more please see: http://weblogs.asp.net/dwahlin/archive/2007/04/29/creating-custom-asp-net-server-controls-with-embedded-javascript.aspx
swartzbill20...
Member
202 Points
102 Posts
Re: migrate javascript to library
Sep 09, 2011 09:18 PM|LINK
I can't get it to work. Here is what I tried.
I added the .js file to the asp.net server control project as an embedded resource. I added the WebResourceAttribute line to AssemblyInfo.cs in the server control project.
Since the original javascript was in the <head> part of Site.master, I added the following to Site.master.cs in the web site project.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string resourceName = "TssbControls.Scripts.dateValidation.js";
ClientScriptManager cs = this.Page.ClientScript;
cs.RegisterClientScriptResource(this.GetType(), resourceName);
}
The js file has a jQuery ready handler in it. I added an alert('HELLO'). It doesn't fire.
Bill
swartzbill20...
Member
202 Points
102 Posts
Re: migrate javascript to library
Sep 09, 2011 10:00 PM|LINK
It now works. I changed the type arg to RegisterClientScriptResource. I randomly picked a class in the server control project. What I really want to do is
<script src='<%# ??????? %>' type="text/javascript" />
where ???????? is triggered by a call to DataBind() in an event handler in the code behind for Site.master. Any ideas?
Steven Cheng...
Contributor
4187 Points
547 Posts
Microsoft
Moderator
Re: migrate javascript to library
Sep 12, 2011 02:42 AM|LINK
Hi swartzbill,
So based on your further description, actually you will want the script resource's url path at runtime so that you can dynamically bind it or assign it to some properties or markup on the page(from your control's code).
I think Decker's original suggestion have made in the right direction since for ASP.NET custom server control( we do not have the entire control over the Page's rendering and output), we need to leverage the "Page.ClientScript"(the script manager) to register some script or reference into the page content.
And instead of the RegisterClientscript method, you can use another method "GetWebResourceUrl". This method can help directly return the url path of a certain script(or other static resource file) which is emit from an embeded resource in control's assembly:
#ClientScriptManager.GetWebResourceUrl Method
http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx
#Embedding Resources in ASP.NET 2.0 Assemblies
http://aspalliance.com/726
BTW, for debugging and tracing such kind of dynamically added script reference, I suggest you use fiddler or the built-in IE develpment toolbar to watch the network trace to see if the dynamic reference is correctly registered on the page(from your server control's code).
#Discovering Internet Explorer Developer Tools
http://msdn.microsoft.com/en-us/library/dd565628(VS.85).aspx
#Fiddler2 - Installation Information
http://www.fiddler2.com/fiddler2/version.asp
Feedback to us
Microsoft One Code Framework
swartzbill20...
Member
202 Points
102 Posts
Re: migrate javascript to library
Sep 12, 2011 02:43 PM|LINK
Decker's reply certainly works. The following also works. In the head section of the master page:
<script src='<%# Page.ClientScript.GetWebResourceUrl(typeof(ServerCtrlProjNameSpace.PickAnyClass), "ServerCtrlProjNameSpace.Scripts.dateValidation.js") %>' type="text/javascript"></script>
The inline data-binding expression is triggered by the following in the load event handler for the master page:
Page.Header.DataBind();