我試圖通過使用ihttpmodule注入js到頁面(到標記)。 但js沒有注入。從IhttpModule注入js
我做了什麼:
頁:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyTempProject._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Temp</title>
</head>
<body>
<form id="form1">
<div>
</div>
</form>
</body>
</html>
的IHttpModule的:
public class MyExtensionModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null)
{
string script = "/Scripts/jquery-1.5.1.js";
if (page.Header != null)
{
string scriptTag = String.Format("<script language=\"javascript\" type=\"text/javascript\" src=\"{0}\"></script>\n", script); page.Header.Controls.Add(new LiteralControl(scriptTag));
}
else if (!page.ClientScript.IsClientScriptIncludeRegistered(page.GetType(), script)) page.ClientScript.RegisterClientScriptInclude(page.GetType(), script, script);
}
}
#endregion
}
以及它永遠不會被添加到page.Header,因爲它不存在(根據您的代碼)。是否有輸出?你是否瀏覽了代碼?你有沒有例外? – jonezy 2011-03-08 23:37:44
不,沒有任何異常..它從來沒有添加scripttag ..所以我怎麼能添加到頭標籤?有什麼想法? – 2011-03-08 23:40:47