Bundle JS files in custom server control

Source: Internet
Author: User
Tags command line empty modify resource
Js|server

Note: This article is based on. NET 2.0 and VS2005

While we are writing Server control with some client-side scripting (JavaScript), it's a problem to publish the script along with the compiled DLL. Writing a paragraph of JavaScript block in a CS file is an "ugly" thing, and JavaScript should stay in the *.js file. How can JS files be "packaged" into DLLs? Check a lot of documents, and finally practice to find that there are a lot of details to be noted. Sorted out, lest everyone detours. No more nonsense, let's get started.

Step 0: What we have
1. Website project:Website1 , which:
Default.aspx (Empty page)
2. WebControl Library project:WebControlLibrary1 , of which:
ClientScriptResourceLabel.cs

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Text;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;

Namespace WebControlLibrary1
{
public class Clientscriptresourcelabel:webcontrol
{

}
} Script_include.jsfunction DoClick () {form1.message.value= ' Text from resource script. '}



Step 1:
In the Properties window of the Script_include.js file, change the build Action to:Embedded Resource


Step 2:

Join in the ClientScriptResourceLabel.cs

[Assembly:webresource ("Script_include.js", "Application/x-javascript")]
Namespace WebControlLibrary1
{
.... Note that this sentence is outside the namespace. You can also add this sentence to theAssemblyInfo.csFile,. NET's class library is unified adds in the AssemblyInfo.cs file.


Many documents (including MSDN) say that the JS file can be bundled into a DLL as a resource through the two steps above. But it's not really going to work. We use reflector to see what is compiled in the end.

How did it become WebControlLibrary1.script_include.js?! The problem is out of the "default namespace." VS will automatically add the default namespace to the resource file. In fact, as long as the default namespace should be empty on it. It is depressing that the official version of VS2005 does not allow the default namespace to be empty (Beta2 is allowed). What do we do? Do you want to compile with a troublesome command line? Another option is to manually modify the project file.

Step 3:
Open the webcontrollibrary1.csproj file with WordPad and change the rootnamespace to Empty

<PropertyGroup>
<configuration condition= "' $ (Configuration) ' = '" >Debug</Configuration>
<platform condition= "' $ (Platform) ' = '" >AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{65431F13-ABAE-4281-A860-90FEC739AFED}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace></RootNamespace>
<AssemblyName>WebControlLibrary1.web</AssemblyName>
</PropertyGroup>


This means that the default namespace is not:



Step 4:
Compile the WebControlLibrary1, which gets what we want:


Step 5
:
Invoke Script resource (ClientScriptResourceLable.cs)

public class Clientscriptresourcelabel:webcontrol
{
protected override void OnPreRender (EventArgs e)
{
if (this. Page!= null)
{
ClientScriptManager Manager1 = this. Page.clientscript;
Manager1. Registerclientscriptresource (typeof (ClientScriptResourceLabel), "Script_include.js");
}
Base. OnPreRender (e);
}
}


Step 6 :
Finally, you can use the wrapped control in the page (Default.aspx):

<%@ Page language= "C #"%>

<%@ Register assembly= "WebControlLibrary1" namespace= "WebControlLibrary1" tagprefix= "CC1"%>
<title>script resource</title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<input type= "text" id= "message" >
<input type= "button" onclick= "DoClick ()" value= "Clientclick" >
<cc1:clientscriptresourcelabel id= "ClientScriptResourceLabel1" runat= "Server"/>
</div>
</form>
</body>

The resulting page is like this:

Script Resource
</title><body>
<form name= "Form1" method= "POST" action= "default.aspx" id= "Form1" >
<div>
<input type= "hidden" name= "__viewstate" id= "__viewstate" value= 05fq= "/>
</div>

<script src= "/website1/webresource.axd?d=e2u_4k_tsvgee7jglgadjyjgqkjj2zwzeqawvi3afwye4ci30ienjer7_ ojolkjr0&t=632688246616562500 "type=" Text/javascript "></script>
<div>
<input type= "text" id= "message" >
<input type= "button" onclick= "DoClick ()" value= "Clientclick" >
<span id= "ClientScriptResourceLabel1" ></span>
</div>
</form>
</body>

One of the <script src= "/website1/webresource.axd?d= ... is a call to a script resource.


Note: In addition to the default namespace will affect the compiled script resource file name, the location of the file is added as a prefix to the file name. For example, you put Script_include.js into the JS directory, compiled it will become JS.scritp_include.js .

# Re: Bundle JS files in custom server control step by step 2005-11-29 09:20 | Pan
In fact, the script into the DLL file, in. NET 1.1 is feasible, the principle, but in 1.1 is to manually modify the project file, but also can be called
StreamReader stream = new StreamReader (assembly.getexecutingassembly (). GetManifestResourceStream ("JS.scritp_include.js"));
Then output the stream to the page.
A mechanism is also provided in. NET 2.0, which is written in AssemblyInfo.cs
[Assembly:webresource ("")], you can also package resources into DLLs.

Reply
# Re: Bundle JS files in custom server control step by step 2005-11-29 10:12 | Ariel Y.
Write in the code.
Manager1. Registerclientscriptresource (typeof (ClientScriptResourceLabel), "WebControlLibrary1.script_include.js");
Can't you?  Is that so troublesome? Reply
# Re: Bundle JS files in custom server control step by step 2005-11-29 11:51 | The shadow of a runaway
@Ariel Y.
It's OK for you to write this, but make the following changes
[Assembly:webresource ("WebControlLibrary1.script_include.js", "Application/x-javascript")]
This can be invoked correctly. I just try to reach the Framework class library effect, System.Web.dll bundle of Detailsview.js, gridview.js, treeview.js, etc. are not prefixed.

@ Pan
I don't know if you read what I wrote. I have written [Assembly:webresource ("...")] added to the AssemblyInfo.cs, or added to the namespace of the class file. And also must modify the JS file build Aciton attribute, but also to ensure that the file into the DLL's JS filename and [Assembly:webresource ("...") to declare the same, this is my core content of this article. Probably not the practice of the classmate is not aware of this, MSDN also did not elaborate. That's why I wrote this article.

Thank you for your attention to my article.  :) Reply


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.