nrvbha nrvbha - 1 month ago 7
ASP.NET (C#) Question

invisible controls in asp.net takes up space in asp.net

I have an asp.net web application. I have one textbox (txtProductName) and one dropdownlist (ddlChanges) visible at the time of page load. Also there is a textbox (txtDeliveryStatus) and a label (lblDeliveryStatus) invisible at page load.

<asp:TextBox ID="txtProductName" runat="server" CssClass="selectstyle" MaxLength="8" Width="60" />

<asp:Label ID="lblDeliveryStatus" runat="server" Style="visibility: hidden;">Delivery Status : </asp:Label>
<asp:TextBox ID="txtDeliveryStatus" runat="server" CssClass="selectstyle" Style="visibility: hidden;"/>

<asp:DropDownList runat="server" ID="ddlChanges" CssClass="selectstyle" onchange="javascript:ddlChangesChanged();">
<asp:ListItem Text="-- Select -- " Value="-1"></asp:ListItem>
<asp:ListItem Text="Status Change" Value="1"></asp:ListItem>
<asp:ListItem Text="Product Name Change" Value="2"></asp:ListItem>
<asp:ListItem Text="Category Change" Value="3"></asp:ListItem>
</asp:DropDownList>


I will make the label and textbox for delivery status visible when user selects Status Change from the dropdown list. But the problem is that the label and textbox for delivery status are invisible and still they take up some space on the page between visible controls.

How to remove that space of invisible controls?

Answer

You should use the display attribute:

<asp:Label ID="lblDeliveryStatus" runat="server" Style="display: none;">Delivery Status : </asp:Label>

<asp:TextBox ID="txtDeliveryStatus" runat="server" CssClass="selectstyle" Style="display: none;"/>

For completeness you can check this link.