开发者

Modify <head> from ASP.NET AJAX

I have recently upgraded to ASP.NET AJAX application. I have two css files in an ASP.NET theme. I use any one of the css per postback depends upon a condition.

Previously with my non-AJAX ASP.NET application, what I used to do is

protected void Page_PreRender(object sender,
                                  EventArgs eventArgs)
    {
        ControlCollection headCollection = Header.Controls;

        for (int i = 0; i < headCollection.Count; i++)
        {
            Control temp = headCollection[i];
            if (temp is HtmlLink)
            {
                if (/* condition to loads a.css */)
                {
                    if (((HtmlLink) temp).Href.EndsWith("a.css", true, null))
                    {
                        headCollection.RemoveAt(i);
                        break;
                    }
                }
                else
                {
                    if (((HtmlLink) temp).Href.EndsWith("b.css", true, null))
                    {
                        headCollection.RemoveAt(i);
                        break开发者_如何学运维;
                    }
                }
            }
        }
    }

But this piece of code does not work when I migrated to ASP.NET AJAX. I have inspected and found that only the loaded css from the first request persists. i.e., if a.css is loaded on the first request, and then b.css is required to be loaded on a specific postback does not work.

Please comment if you are confused about the problem.


I think that you use UpdatePanel. And second postback is executed in partial rendering mode. You cannot change Head control during such postback. But you can try to register startup script by using ScriptManager: ScriptManager.RegisterStartupScript. And change head tag by javascript.


Thanks Unholy, solved the problem using jQuery and selector.

Just used this:

$("head > link[href$='a.css']").remove();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜