C# 如何防止JS文件 不被下载

本文介绍如何将JavaScript文件作为嵌入资源添加到ASP.NET项目中,并通过注册这些资源使其能在页面加载时正确引用。

解决方案资源管理器中,右键查看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);
           }

       }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值