惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
J
Java Code Geeks
雷峰网
雷峰网
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
Vulnerabilities – Threatpost
S
Securelist
The Hacker News
The Hacker News
The Register - Security
The Register - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
G
Google Developers Blog
Hugging Face - Blog
Hugging Face - Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
M
MIT News - Artificial intelligence
AI
AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Schneier on Security
Schneier on Security
N
Netflix TechBlog - Medium
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hacker News: Front Page
博客园 - 司徒正美
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
大猫的无限游戏
大猫的无限游戏
Security Latest
Security Latest
Engineering at Meta
Engineering at Meta
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
U
Unit 42
V
V2EX
V2EX - 技术
V2EX - 技术
L
LINUX DO - 最新话题
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
Recorded Future
Recorded Future
P
Privacy & Cybersecurity Law Blog
美团技术团队
小众软件
小众软件
F
Fortinet All Blogs

博客园 - ㊣鑫哥

jQuery插件---获取URL参数. 利用反射,泛型,扩展方法快速获取表单值到实体类 [原创]jQuery PreviewImg 上传预览插件 js操作frameset frame 对象 关于“Internet Explorer无法打开站点,已终止操作” JavaScript 面向对象程序设计(下)——继承与多态 JavaScript 面向对象程序设计(上)——封装[转] JS类 检测上传图片的大小,宽,高及格式检查 .net 2.0 制作 柱状图 [转]AJAX之旅 由prototype_1.3.1进入javascript殿堂-类的初探 asp.net如何获取客户端网卡mac地址 [转]不可多得的Javascript(AJAX)开发工具 - Aptana 探究 Singleton 设计模式(构建分布式应用程序) .Net平台下开发中文语音应用程序[转] IE下JavaScript迁移到FireFox下的工作笔记[转] Asp.net组件开发(资料贴) 有关于JSON的一些资料 这样的UpdatePanel控件要怎么做 [转]30岁前男人应该做的16件事
[转]在自定义Server Control中捆绑JS文件 Step by Step
㊣鑫哥 · 2007-04-17 · via 博客园 - ㊣鑫哥

原:http://jackielin.cnblogs.com/archive/2005/11/29/286570.html

注:本文基于.NET 2.0 和 VS2005

我们在编写 Server Control 的时候难免要用到一些客户端脚本(javascript),如何把脚本和编译好的dll一起发布就成了一个问题。把一段一段的javascript block写在cs文件里是一件很“丑陋”的事情,javascript就应呆在*.js文件里。js文件怎样才能“打包”到dll里呢?查了很多文档,最后实践下来发现有很多细节是需要注意的。整理出来,免得大家走弯路。废话无多,让我们开始。

Step 0: 我们已有的
1. 网站项目:Website1 ,其中:  
        Default.aspx (空页面)
2. WebControl库项目:WebControlLibrary1 ,其中: 
        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:
在script_include.js文件的属性窗口里,把Build Action改为:Embedded Resource
 


Step 2:

ClientScriptResourceLabel.cs 中加入

[assembly: WebResource("script_include.js""application/x-javascript")]
namespace WebControlLibrary1
{
....

注意这句是在namespace之外。你也可以把这句加在AssemblyInfo.cs文件里,.NET的类库就是统一加在AssemblyInfo.cs文件里的。

很多文档(包括MSDN)里都说通过以上两步就可以把js文件作为资源,捆绑到dll中了。但实际上更本就不能用。我们用Reflector来看看编译出来的到底是什么东东。

怎么变成 WebControlLibrary1.script_include.js 了?!问题是出在“默认名称空间”。VS会自动把default namespace加到资源文件前面。其实只要把默认名称空间改为空就可以了。令人郁闷的是VS2005正式版不允许把default namespace改为空(beta2是允许的)。怎么办呢?难道要用麻烦的命令行来编译吗?还有一个办法就是手工修改项目文件。

Step 3:
用写字板打开WebControlLibrary1.csproj文件,把其中的RootNamespace 改为空

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

这样一来“默认名称空间”就没有了:

Step 4:
编译WebControlLibrary1,这下得到的是我们想要的了:


Step 5
: 
调用脚本资源(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
终于可以在页面里使用包装好的控件了(Default.aspx):

<%@ Page Language="C#" %> <%@ Register Assembly="WebControlLibrary1" Namespace="WebControlLibrary1" TagPrefix="cc1" %>
<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">
            
<cc1:ClientScriptResourceLabel ID="ClientScriptResourceLabel1" runat="server" />
        
</div>
    
</form>
</body>
</html>

生成的页面是这样的:

<html>
<head><title>
    Script Resource
</title></head>
<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=E2u_4K_tSvgEe7jglgaDJYjGQkJj2ZwZEqAWVi3afWYe4CI30IeNjer7_ojoLKjr0&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=...... 就是对脚本资源的调用。

注意:除了default namespace会影响编译出来的脚本资源文件名外,文件所在的位置也会作为前缀加到文件名上。例如你把script_include.js放到 JS 目录下,编译出来就会变成 JS.scritp_include.js