问题
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:
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.
回答2:
Thanks Unholy, solved the problem using jQuery and selector.
Just used this:
$("head > link[href$='a.css']").remove();
来源:https://stackoverflow.com/questions/1880138/modify-head-from-asp-net-ajax