问题描述
我在 UpdatePanel 中有一个用户控件.一旦触发事件并更新用户控件 - 它似乎失去了它的 css 样式.这仅在 IE8 中发生在我身上,而在 Chrome 和 FF 中则很好.
I have an user control inside an UpdatePanel. Once an event is triggered and updates the user control - it seems to lose its css styles. This happened to me in IE8 only, while in Chrome and FF it was fine.
例如一个用户控件:
<style type="text/css">
div.test
{
width: 500px;
height: 400px;
background-color: #0000BB;
color: #FFFFFF;
border: 2px solid #11CC00;
}
</style>
<div id="div123" runat="server" class="test"></div>
public void SetText(string text)
{
div123.InnerText = text;
}
在 UpdatePanel 中使用用户控件并对其进行更新的页面:
A page using the user control inside an UpdatePanel and updating it:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div id="div1" runat="server">
<uc:TestUC ID="test1" runat="server"></uc:TestUC>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_OnClick" Text="Click" />
</ContentTemplate>
</asp:UpdatePanel>
protected void Button1_OnClick(object sender, EventArgs e)
{
test1.SetText(DateTime.Now.ToString());
}
在 Chrome 和 FF 中,它似乎按预期工作(按钮单击导致当前时间显示在用户控件中,没有其他反应),但在 IE8 中,用户控件内的 div 失去其样式(背景颜色、边框).
In Chrome and FF it seems to be working as expected (button click causes current time to display in the user control, nothing else happens), but in IE8 the div inside the user control loses its styles (background color, borders).
什么可能导致这个问题,可以做些什么来防止它?
What could be causing this problem, and what can be done to prevent it?
推荐答案
提到这个问题这里.
我尝试了这个建议 - 在 OnInit 中注册 css 链接 - 它似乎有效.
I tried the suggestion - registering the css link in the OnInit - and it seems to work.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ScriptManager sm = ScriptManager.GetCurrent(Page);
if (!sm.IsInAsyncPostBack)
{
string css = string.Format("<link rel="stylesheet" href="{0}" type="text/css" />", ResolveUrl(CssClassFile));
ScriptManager.RegisterClientScriptBlock(this, typeof(MyBlahControl), "MyBlahId", css, false);
}
}
这篇关于UpdatePanel 中的用户控件 - 更新时 css 样式消失(IE8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!