Dzwebs.Net

撰写电脑技术杂文十余年

如何将JS文件编译到ASP.NET项目里的DLL文件中

Admin | 2009-6-12 19:33:08 | 被阅次数 | 7026

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  ①向项目中添加Jscript文件

  //script_1.js-----
  function doClick1()
  {
      alert("OK1_wufeng");
  }
  //script_2.js-----
  function doClick2()
  {
      alert("OK2");
  }

  ②解决方案资源管理器中,右键查看script_1.js和script_2.js的属性,把高级中的“生成操作”属性设置成“嵌入的资源”。

  ③向AssemblyInfo.cs文件中添加如下行:(注意域名wf.ClientScriptResourceLabel)

[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_1.js", "application/x-javascript")]
[assembly: System.Web.UI.WebResource("wf.ClientScriptResourceLabel.script_2.js", "application/x-javascript")]

  ④向项目中添加一个类, 实例:

using System;
using System.Drawing;
using System.Web.UI;
using System.Web;
using System.Globalization;

namespace wf.ClientScriptResourceLabel
{
    public class ClientScriptResourceLabel : System.Web.UI.WebControls.WebControl
    {
        //调用脚本资源
        protected override void OnPreRender(EventArgs e)
        {
            if (this.Page != null)
            {
                this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_1.js");
                this.Page.ClientScript.RegisterClientScriptResource(typeof(ClientScriptResourceLabel), "wf.ClientScriptResourceLabel.script_2.js");
            }
            base.OnPreRender(e);
        }
        /// <summary>
        /// 呈现控件的方法RenderContents
        /// </summary>
        protected override void RenderContents(HtmlTextWriter output)
        {
            output.AddAttribute("id", "1");
            output.AddAttribute("type", "checkbox");
            output.AddAttribute("value", "测试1");
            output.AddAttribute("onclick", "javascript:doClick1();");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();

            output.AddAttribute("id", "2");
            output.AddAttribute("type", "checkbox");
            output.AddAttribute("value", "测试2");
            output.AddAttribute("onclick", "javascript:doClick2();");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            output.RenderEndTag();
         base.RenderContents(output);
        }
    }
}


该杂文来自: 网站开发杂文

上一篇:根据URL自动生成网页缩略图的源代码

下一篇:Crystal Reports编程之基本语法介绍

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计