开发者

asp.net - load CSS file dynamically renders but doesn't load the CSS file

in my ASP.NET app, I'm trying to add a CSS reference dynamically (with intelligent caching on the client).

My code renders to the browser correctly, but it doesn't load the CSS file. I have to put the link in my markup explicitly for the CSS file to load, but开发者_开发知识库 it won't load when in code behind

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        FileInfo fi = new FileInfo(Server.MapPath("~/Styles/Site.css"));
        HtmlGenericControl link = new HtmlGenericControl("LINK");
        link.Attributes.Add("rel", "stylesheet");
        link.Attributes.Add("type", "text/css");
        link.Attributes.Add("href", "~/Styles/Site.css?t=" + fi.CreationTime.Ticks.ToString());
        Page.Header.Controls.Add(link);
    }
}

This seems really simple. Not sure why it isn't working.

NOTE: the attending the fileinfo ticks has no effect on the behavior. It renders fine but doesn't load regardless of appending the file information.


You need to resolve the client URL for the stylesheet and then it will work:

link.Attributes.Add("href", ResolveClientUrl("~/Styles/Site.css?t=" + fi.CreationTime.Ticks));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜