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

ASP.NET
Web服务器控件:LinkButton控件
Web服务器控件:ListBox控件
Web服务器控件:ListItem控件
Web服务器控件:Literal控件
Web服务器控件:Panel控件
Web服务器控件:PlaceHolder控件
Web服务器控件:RadioButton控件
Web服务器控件:RadioButtonList控件
Web服务器控件:BulletedList控件
Web服务器控件:Style控件
Web服务器控件:Table控件
Web服务器控件:TableCell控件
Web服务器控件:TableRow控件
Web服务器控件:TextBox控件
Web服务器控件:XML控件
Validation服务器控件:CompareValidator控件
Validation服务器控件:CustomValidator控件
Validation服务器控件:RangeValidator控件
Validation服务器控件:RegularExpressionValidator控件
Validation服务器控件:RequiredFieldValidator控件

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


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