Taming Ajaxified asp:HiddenField Controls in Internet Explorer

Tuesday, December 09, 2008 by ASP.NET AJAX Team | Comments 3

If you are using asp:HiddenField controls (rendered as <input type="hidden" />) in your website and update their values with AJAX requests, you may observe some unexpected changes in the page layout in Internet Explorer (IE). It will seem that an empty line is inserted at each location where a HiddenField control resides. The behavior is observed only in IE and looks like a problem caused by the browser or the MS AJAX client-side framework.

A quite easy workaround for this glitch is to wrap the HiddenField in a container with no height and reduced font size. Here is an example:

 

<%@ Page Language="C#" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head runat="server"
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>Ajaxified asp:HiddenField in Internet Explorer</title> 
<style type="text/css"
 
.HiddenFieldDiv 
    font-size:1px; 
    height:0; 
 
</style> 
</head> 
<body> 
<form id="form1" runat="server"
<asp:ScriptManager ID="ScriptManager1" runat="server" /> 
 
<p>Below this paragrapgh there are two ajaxified asp:HiddenField controls, separated by 
horizontal rules. <strong>Internet Explorer</strong> expands those after an AJAX request, so 
a little CSS workaround is required in order to preserve the page layout.</p> 
 
<hr /> 
 
<asp:UpdatePanel ID="UpdatePanel1" runat="server"
<ContentTemplate> 
    <asp:HiddenField ID="HiddenField1" runat="server" Value="1" /> 
</ContentTemplate> 
<Triggers> 
    <asp:AsyncPostBackTrigger ControlID="Button1" /> 
</Triggers> 
</asp:UpdatePanel> 
 
<hr /> 
 
<div class="HiddenFieldDiv"
<asp:UpdatePanel ID="UpdatePanel2" runat="server"
<ContentTemplate> 
    <asp:HiddenField ID="HiddenField2" runat="server" Value="1" /> 
</ContentTemplate> 
<Triggers> 
    <asp:AsyncPostBackTrigger ControlID="Button1" /> 
</Triggers> 
</asp:UpdatePanel> 
</div> 
 
<hr /> 
 
<p><asp:Button ID="Button1" runat="server" Text="Initiate Ajax Request" /></p
 
<p>The second HiddenField control is wrapper inside a div, which has its font-size set to 1px 
and the height is 0px. This prevents the HiddenField control from expanding in IE.</p> 
 
</form> 
</body> 
</html> 
 

 

3 Comments

  • Anders M. 11 Dec

    Hi, thanks.. This also happens for the div with the class name rpTemplate inside a panel item in the panel bar on ajax update with the ajax manager (updating 1-# controls). Workaround:

    .rpTemplate {
     line-height: 0px !important;
    }

  • Philip 13 Dec
    You can also make the containers display to none?
  • Dimo Dimov 15 Dec
    Hi Philip,

    Yes, surely you can also hide the HiddenField's container completely with display:none. I am a little surprised I didn't think of that myself :)

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)