当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 控件开发时两种JS嵌入资源方式的使用方法

ASP.NET
部署ASP.NET的三大技术(2)
部署ASP.NET的三大技术(3)
部署ASP.NET的三大技术(4)
部署ASP.NET的三大技术(5)
部署ASP.NET的三大技术(6)
ASP.NET可交互式位图窗体设计(1)
ASP.NET可交互式位图窗体设计(2)
ASP.NET可交互式位图窗体设计(3)
ASP.NET可交互式位图窗体设计(4)
ASP.NET可交互式位图窗体设计(5)
ASP.NET可交互式位图窗体设计(6)
ASP.NET可交互式位图窗体设计(7)
ASP.NET可交互式位图窗体设计(8)
ASP.NET可交互式位图窗体设计(9)
ADO.NET2.0的十大新特性
用Asp.net实现基于XML的留言簿之一
用Asp.net实现基于XML的留言簿之二
用Asp.net实现基于XML的留言簿之三
用Asp.net实现基于XML的留言簿之四
在ASP.NET中使用.NET组件

ASP.NET 中的 控件开发时两种JS嵌入资源方式的使用方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 242 ::
收藏到网摘: n/a

第一种:
直接把要嵌入的JS文件属性设置为“嵌入的资源”。
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
if(!base.Page.IsStartupScriptRegistered("Script"))
{
Assembly assembly = typeof(TestControl).Assembly;
StreamReader reader = null;
reader = new StreamReader(assembly.GetManifestResourceStream(typeof(TestControl),"test.js"));
base.Page.RegisterStartupScript("Script",reader.ReadToEnd());
}
}
第二种:
创建相同类名的资源文件,然后在Data出的name设置为Test,value为js的内容。
protected override void OnInit(EventArgs e)
{
base.OnInit (e);
if(!base.Page.IsStartupScriptRegistered("Script"))
{
ResourceManager resx = new ResourceManager(base.GetType());
base.Page.RegisterStartupScript("Script",resx.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture,true,true).GetString("Test"));
}
}
上面两种方式都是在控件类中输出JS代码,也可以自定义HttpHandler来生成Js代码。