Asp.net package js files into dll

Source: Internet
Author: User

Asp.net package js files into the dll Method for favorites
Note: This article is based on. NET 2.0 and VS2005.

When writing Server Control, we will inevitably use some client scripts (javascript). How to publish the script together with the compiled dll becomes a problem. Writing a section of javascript block in a cs file is an ugly thing. javascript should stay in the *. js file. How can I package js files into dll files? I checked a lot of documents and found that there are many details that need attention. Sort it out to avoid detours. Let's get started.

Step 0: we already have
1. website project: Website1, where:
Default. aspx (empty page)
2. WebControl Library Project: WebControlLibrary1, where:
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.js

Function DoClick () {Form1.Message. value = 'text from resource script .'}
 

Step 1:
In the attribute window of the script_include.js file, Change Build Action to Embedded Resource.
 

Step 2:
Add ClientScriptResourceLabel. cs

[Assembly: WebResource ("script_include.js", "application/x-javascript")]
Namespace WebControlLibrary1
{
....
Note that this sentence is outside of namespace. You can also add this sentence to the AssemblyInfo. cs file. the. NET class library is uniformly added to the AssemblyInfo. cs file.

Many documents (including MSDN) refer to the above two steps to use js files as resources and bind them to the dll. But it is not actually usable. Let's use Reflector to see what the compilation is.
 
How does it become WebControlLibrary1.script _ include. js ?! The problem lies in "default namespace ". VS automatically adds the default namespace to the front of the resource file. In fact, you only need to leave the default namespace empty. It is depressing that the official VS2005 version does not allow changing the default namespace to null (beta2 is allowed ). What should we do? Is it necessary to compile with troublesome command lines? Another way is to manually modify the project file.

Step 3:
Open the WebControlLibrary1.csproj file on the WordPad and change the RootNamespace to null.

<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>

In this way, the "default namespace" is gone:
 

Step 4:
Compile WebControlLibrary1 to get what we want:
 

Step 5:
Call the 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 encapsulated control (Default. aspx) on the page ):

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

<% @ Register Assembly = "WebControlLibrary1" Namespace = "WebControlLibrary1" TagPrefix = "PC3" %>
<Html>
<Head runat = "server">
<Title> Script Resource </title>
</Head>
<Body>
<Form id = "Form1" runat = "server">
<Div>
<Input type = "text" id = "Message">
<Input type = "button" onclick = "DoClick ()" value = "ClientClick">
<PC3: ClientScriptResourceLabel ID = "ClientScriptResourceLabel1" runat = "server"/>
</Div>
</Form>
</Body>
</Html>
The generated page is as follows:

<Html>
<Head> <title>
Script Resource
</Title> <Body>
<Form name = "Form1" method = "post" action = "Default. aspx" id = "Form1">
<Div>
<Input type = "hidden" name = "_ VIEWSTATE" id = "_ VIEWSTATE" value = "/wEPDwUKLTkwOTU4NDc0OGRkO0UjKICXV1XisDv/KKM/wA + 05FQ ="/>
</Div>

<Script src = "/WebSite1/WebResource. axd? D = plain & amp; 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>
</Html>
<Script src = "/WebSite1/WebResource. axd? D =... is the call to the script resource.

Note: In addition to the default namespace, the compiled script resource file name is affected, and the file location is also prefixed to the file name. For example, if you put script_include.js under the JS directory and compile it, it will become JS. scritp_include.js.

Related Article

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.