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

ASP.NET
HTML服务器控件介绍:HtmlInputFile控件
HTML服务器控件介绍:HtmlInputHidden控件
HTML服务器控件介绍:HtmlInputImage控件
HTML服务器控件介绍:HtmlInputRadioButton控件
HTML服务器控件介绍:HtmlInputText控件
HTML服务器控件介绍:HtmlSelect控件
HTML服务器控件介绍:HtmlTable控件
HTML服务器控件介绍:HtmlTableCell控件
HTML服务器控件介绍:HtmlTableRow控件
HTML服务器控件介绍:HtmlTextArea控件
Web服务器控件:AdRotator控件
Web服务器控件:Button控件
Web服务器控件:Calendar控件
Web服务器控件:CheckBox控件
Web服务器控件:CheckBoxList控件
Web服务器控件:DropDownList控件
Web服务器控件:HyperLink控件
Web服务器控件:Image控件
Web服务器控件:ImageButton控件
Web服务器控件:Label控件

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


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