JScript.js的内容为:
function funMessageShow() {
alert("测试加载Js");
}
StyleSheet.css的内容为:
.testCss
{
background-color:Gray;
}
前端页面的html源码为:
<form id="form1" runat="server">
<div class="testCss">
测试加载Css
</div>
<input id="btnShow" type="button" value="测试加载js" onclick="return funMessageShow();" />
</form>
现在要从后台动态加载它们。
加载Css的主要代码为:
代码
/// <summary>
/// 正常加载CSS
/// </summary>
/// <param name="pPage">要加载的页面</param>
/// <param name="sStyleLink">样式url</param>
public void LinkStyle(Page pPage, string sStyleLink)
{
System.Web.UI.HtmlControls.HtmlLink hlLink = new System.Web.UI.HtmlControls.HtmlLink();
hlLink.Attributes.Add("rel", "stylesheet");
hlLink.Attributes.Add("type", "text/css");
hlLink.Attributes.Add("href", sStyleLink);
Page.Header.Controls.Add(hlLink);
}
加载Js的代码为 :
代码
/// <summary>
/// 正常加载Js
/// </summary>
/// <param name="pPage">要加载的页面</param>
/// <param name="sJsUrl">js的url</param>
public void LinkJs(Page pPage, string sJsUrl)
{
System.Web.UI.HtmlControls.HtmlGenericControl hgcLoadJs = new System.Web.UI.HtmlControls.HtmlGenericControl();
hgcLoadJs.TagName = "script";
hgcLoadJs.Attributes.Add("type", "text/javascript");
hgcLoadJs.Attributes.Add("src", Page.ResolveClientUrl(sJsUrl)); //ResolveClientUrl:获取浏览器可以使用的 URL
Page.Header.Controls.Add(hgcLoadJs);
}
后台完整的代码如下,包含对是否已加载的判断代码。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string sThisJsUrl = "JScript.js";
string sThisStyleUrl = "StyleSheet.css";
LinkStyle(this.Page, sThisStyleUrl);
LinkJs(this.Page, sThisJsUrl);
//ReferenceAjaxScript(this.Page, sThisJs);
//ReferenceJs(this.Page, sThisJs, sThisJs);
judgeCssExist(sThisStyleUrl);
judgeJsExist(sThisJsUrl);
}
}
#region 加载Css
/// <summary>
/// 正常加载CSS
/// </summary>
/// <param name="pPage">要加载的页面</param>
/// <param name="sStyleLink">样式url</param>
public void LinkStyle(Page pPage, string sStyleLink)
{
System.Web.UI.HtmlControls.HtmlLink hlLink = new System.Web.UI.HtmlControls.HtmlLink();
hlLink.Attributes.Add("rel", "stylesheet");
hlLink.Attributes.Add("type", "text/css");
hlLink.Attributes.Add("href", sStyleLink);
Page.Header.Controls.Add(hlLink);
}
//加css不同法,功能与上边方法相同
public void LinkStyle2(Page pPage, string sStyleLink)
{
System.Web.UI.HtmlControls.HtmlGenericControl objLink = new System.Web.UI.HtmlControls.HtmlGenericControl();
objLink.TagName = "link";
objLink.Attributes["rel"] = "stylesheet";
objLink.Attributes["type"] = "text/css";
objLink.Attributes["href"] = sStyleLink;
Page.Header.Controls.Add(objLink);
}
#endregion
#region 加载Js
/// <summary>
/// 正常加载Js
/// </summary>
/// <param name="pPage">要加载的页面</param>
/// <param name="sJsUrl">js的url</param>
public void LinkJs(Page pPage, string sJsUrl)
{
System.Web.UI.HtmlControls.HtmlGenericControl hgcLoadJs = new System.Web.UI.HtmlControls.HtmlGenericControl();
hgcLoadJs.TagName = "script";
hgcLoadJs.Attributes.Add("type", "text/javascript");
hgcLoadJs.Attributes.Add("src", Page.ResolveClientUrl(sJsUrl)); //ResolveClientUrl:获取浏览器可以使用的 URL
Page.Header.Controls.Add(hgcLoadJs);
}
/// <summary>
/// 加载js
/// </summary>
/// <param name="pPage">要加载的页面</param>
/// <param name="sJsName">js名称</param>
/// <param name="sJsUrl">js的Url</param>
public void ReferenceJs(Page pPage,string sJsName,string sJsUrl)
{
Type tPage = Page.GetType();
ClientScriptManager csmManager = Page.ClientScript;
if (!csmManager.IsClientScriptIncludeRegistered(tPage, sJsName))
{
csmManager.RegisterClientScriptInclude(tPage, sJsName, Page.ResolveClientUrl(sJsUrl));
}
}
#endregion
/// <summary>
/// 在Ajax方式下加载Js
/// </summary>
/// <param name="pPage">要加载的页面</param>
/// <param name="sJsUrl">js的url</param>
public void ReferenceAjaxScript(Page pPage, string sJsUrl)
{
ScriptReference srReference = new ScriptReference(sJsUrl);
ScriptManager smManager = ScriptManager.GetCurrent(pPage);
if (smManager != null)
{
smManager.Scripts.Add(srReference);
}
}
/// <summary>
/// 判断Css是否已加载
/// </summary>
/// <param name="sCssUrl">css url</param>
private void judgeCssExist(string sCssUrl)
{
ControlCollection ccCollection = Page.Header.Controls;
if (ccCollection.Count > 0)
{
foreach (Control cItem in ccCollection)
{
if (cItem.GetType() == typeof(System.Web.UI.HtmlControls.HtmlLink))
{
System.Web.UI.HtmlControls.HtmlLink hlLink = cItem as System.Web.UI.HtmlControls.HtmlLink;
if (hlLink.Attributes["href"].ToString() == sCssUrl)
{
Response.Write("此CSS已存在,无须加载!<br/>");
}
}
}
}
}
/// <summary>
/// 判断js是否已加载
/// </summary>
/// <param name="sJsUrl">js url</param>
private void judgeJsExist(string sJsUrl)
{
ControlCollection ccCollection = Page.Header.Controls;
if (ccCollection.Count > 0)
{
foreach (Control cItem in ccCollection)
{
if (cItem.GetType() == typeof(System.Web.UI.HtmlControls.HtmlGenericControl))
{
System.Web.UI.HtmlControls.HtmlGenericControl hgcControl = cItem as System.Web.UI.HtmlControls.HtmlGenericControl;
if (hgcControl.Attributes["src"].ToString() == sJsUrl)
{
Response.Write("此Js已存在,无须加载!<br/>");
}
}
}
}
}
}
另外一篇文章摘录,相同的主题
.net动态加载JS与CSS与生成标签2010-09-03 15:31转载自 ssyj00最终编辑 ssyj00using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//动态加载外部CSS文件
HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
link.Href = "不知道什么路径" + "/style.css";
this.Page.Header.Controls.Add(link);
//动态加载外部JS文件
Page.ClientScript.RegisterClientScriptInclude("Common", "/UserPart/Script/Common.js");
this.Page.ClientScript.RegisterClientScriptInclude(this.Page.GetType(), "Common", "/UserPart/Script/Common.js");
//动态生成DIV层
HtmlGenericControl div1 = new HtmlGenericControl();
div1.TagName = "div";
div1.Attributes.Add("id", "layout");
div1.Style.Add("width","100%");
div1.InnerHtml = "<h1>测试能否显示</H1>";
StringBuilder str = new StringBuilder();
str.Append("<div id=\"ssyj\"onclick=\"alert('123ok')\";>jidfj</div>");
div1.InnerHtml = str.ToString();
this.Page.FindControl("form1").Controls.Add(div1);
//动态生成表格
HtmlTable talbe = new HtmlTable();
talbe.Attributes.Add("borde", "1");
talbe.Attributes.Add("id", "tid");
this.Page.Title = "向页面发送控件元素";
this.Page.FindControl("form1").Controls.Add(talbe);
}