Controls inside UpdatePanel - css styles disappear on update (IE8)

I have a user control inside an UpdatePanel. As soon as the event is fired and the user control is updated - it seems to lose css styles. This only happened to me in IE8, while in Chrome and FF everything was fine.

User control, for example:

<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; } 

Page using user control inside 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()); } 

In Chrome and FF, it works as expected (pressing the button causes the current time to be displayed in the user control, nothing else happens), but in IE8 the div inside the user control loses its styles (background color, border).

What could be causing this problem, and what could be done to prevent it?

+4
source share
4 answers

This question is mentioned here .

I tried the suggestion - register a css link in 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); } } 
+3
source

I had a similar problem.
I decided to move css <link .. from <page> to <header> .

Since I use MasterPage, and I don’t need a link on all pages, I found the ContentPlaceHolder useful place in the HeadPage Header

 <head id="Head1" runat="server"> ... <asp:ContentPlaceHolder ID="HeaderContentPlaceHolder" runat="server"/> </head> 

and then the link inside the desired page:

 <asp:Content ID="Content2" ContentPlaceHolderID="HeaderContentPlaceHolder" Runat="Server"> <link rel="stylesheet" type="text/css" href="CSS/my.css" /> </asp:Content> 
+1
source

Copy css from the user control to the page to solve this problem.

0
source

I had the same problem with my application, it seemed to kill all the styles in my dynamic panel ... I fixed it by moving the content placeholder (the place where I write dynamic HTML) to <head> on my main page.

Working! Thanks everyone :)

0
source

Source: https://habr.com/ru/post/1337652/


All Articles