views:

67

answers:

2

Guys,

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.

+1  A: 

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.

Unholy
+1  A: 

Thanks Unholy, solved the problem using jQuery and selector.

Just used this:

$("head > link[href$='a.css']").remove();
Munim Abdul
Don't forget to mark a valid answer ;)
Unholy