技术开发 频道

动态引入js文件和css样式表文件


【IT168技术文档】

  在开发web项目的过程中,总会碰到用户控件 .ascx 和页面,或者页面和母版页的很多关系。用局部的页面

  动态地给母版页引入js文件或者样式表文件,或者在用户控件里面根据需要动态引入js文件和样式表会比较好。

  以下是我引入的一个方法:
// 引入js文件 HtmlGenericControl scriptControl = new HtmlGenericControl("script"); scriptControl.Attributes.Add("type", "text/javascript"); scriptControl.Attributes.Add("language", "JavaScript"); scriptControl.Attributes.Add("src", "http://images.dayoo.com/travel/9208.files/changeimg.js"); Page.Header.Controls.Add(scriptControl); // 引入样式表 Literal cssControl = new Literal(); cssControl.Text = "<link href=\"http://images.dayoo.com/photo/17358.files/css.css\" rel=\"stylesheet\" type=\"text/css\"/>"; Page.Header.Controls.Add(cssControl);
  以上代码在页面或者控件的PageLoad里面。
0
相关文章