C#实现动态引用外部的Javascript,CSS

不多说,贴代码。

using System;
using System.Web.UI.HtmlControls;
namespace webtest
{
    public partial class Aspx : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //动态加载javascript
            HtmlGenericControl js = new HtmlGenericControl("script");
            js.Attributes.Add("type", "text/javascript");
            js.Attributes.Add("src", "/Scripts/commonjs.js");
            Page.Header.Controls.AddAt(1, js);
            //动态加载css
            HtmlLink css = new HtmlLink();
            css.Href = "/Styles/site.css";
            css.Attributes.Add("rel", "stylesheet");
            css.Attributes.Add("type", "text/css");
            Page.Header.Controls.AddAt(1, css);
        }

    }
}
View Code

动态生成的HTML

<!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>

</title><link href="/Styles/site.css" rel="stylesheet" type="text/css" /><script type="text/javascript" src="/Scripts/commonjs.js"></script>
        <script type="text/javascript">
            var Request = GetRequest();
            if (Request["id"] != undefined)
                alert(Request["id"]);
    </script>
</head>
<body>
    <div>
        <a onclick="location.href='aspx.aspx?id=3&wo=me'">点击我</a>
    </div>
</body>
</html>
View Code

 

posted @ 2013-05-31 11:10  编程小帆  阅读(657)  评论(0编辑  收藏  举报