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

ASP.NET
ASP.NET入门教程:ArrayList对象
ASP.NET入门教程:Hashtable对象
ASP.NET入门教程:SortedList对象
ASP.NET入门教程:把XML文件绑定到列表控件
ASP.NET入门教程:Repeater控件
ASP.NET入门教程:DataList控件
ASP.NET入门教程:数据库连接
ASP.NET入门教程:ASP.NET 2.0新特性
ASP.NET入门教程:ASP.NET 2.0母版页
ASP.NET入门教程:ASP.NET 2.0导航
ASP.NET入门教程:HTML服务器控件
ASP.NET入门教程:Web服务器控件
ASP.NET入门教程:Validation服务器控件
HTML服务器控件介绍:HtmlAnchor控件
HTML服务器控件介绍:HtmlButton控件
HTML服务器控件介绍:HtmlForm控件
HTML服务器控件介绍:HtmlGeneric控件
HTML服务器控件介绍:HtmlImage控件
HTML服务器控件介绍:HtmlInputButton控件
HTML服务器控件介绍:HtmlInputCheckBox控件

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 152 ::
收藏到网摘: 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代码。