locked
will setting div visibility to false remove server control? RRS feed

  • Question

  • User-2146352328 posted

    Hi

    < div style="visibility="false" " >

    <asp label runat=server/>

    </div>

    Will the server label by available on server calls?

    If not, is there a workaround for the div ?

    Thanks.

    Friday, March 1, 2019 7:24 PM

Answers

  • User475983607 posted

    sapator

    It's not that complicated

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="visibility:hidden">
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        </form>
    </body>
    </html>

    LAbel will always be caught on server code even if I use yle="visibility:hidden"

    Correct, and the expected behavior.  Can you explain the problem you are trying to solve?

    Perhaps you want to disable ViewState?

        <form runat="server">
            <div style="display:block">
                <asp:Label ID="srcDisplayNone" runat="server" EnableViewState="false"></asp:Label>
            </div>
            <div style="visibility:inherit">
                <asp:Label ID="srcVisibilityHidden" runat="server" EnableViewState="false"></asp:Label>
            </div>
            <div>
                <asp:Label ID="DisplayNone" runat="server"></asp:Label>
            </div>
            <div>
                <asp:Label ID="VisibilityHidden" runat="server"></asp:Label>
            </div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </form>
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    srcDisplayNone.Text = "Hello from display:none";
                    srcVisibilityHidden.Text = "Hello from visibility:hidden";
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                DisplayNone.Text = srcDisplayNone.Text;
                VisibilityHidden.Text = srcVisibilityHidden.Text;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2019 9:21 PM
  • User-943250815 posted

    sapator,

    You can hide your DIV using visibility: hidden or display:none, the only difference between both is when you use visibility the hidden element take space on page
    https://www.w3schools.com/cssref/pr_class_visibility.asp

    You can set any CSS on your DIV, controls inside will be rendered on page anyway, any CSS definition apply only on how Browser will render page, but not page contents

    On the other hand from code-behind you can set your label visible or not, in this case you defining if your label should be part of page or not

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2019 11:00 PM

All replies

  • User475983607 posted

    sapator

    Hi

    < div style="visibility="false" " >

    <asp label runat=server/>

    </div>

    Will the server label by available on server calls?

    If not, is there a workaround for the div ?

    Thanks.

    The code shown is completely invalid. 

    However, when the server control's Visible property is set to false, the control is not rendered.  The common solution is CSS.

    style="display:none"

    Friday, March 1, 2019 7:35 PM
  • User-2146352328 posted

    Hi.

    Yes I wrote the code fast, sorry about that but you got the idea.

    Actually the div is not a server control but includes server controls.

    If I do on the div : style="visibility:hidden" it will still get the label although it is not shown on page.

    Thanks.

    Friday, March 1, 2019 7:55 PM
  • User475983607 posted

    sapator

    Yes I wrote the code fast, sorry about that but you got the idea.

    Please post the actual code that causes the unexpected results.  It's very difficult to infer intention when the code has obvious errors.  

     

    sapator

    Actually the div is not a server control but includes server controls.

    If I do on the div : style="visibility:hidden" it will still get the label although it is not shown on page.

    I'm a bit confused are you asking a question or after you cleaned up the invalid code, you are getting the expected results?  What is the purpose of this post?

    Friday, March 1, 2019 8:09 PM
  • User-2146352328 posted

    Hi.

    I'm getting the code but I want to clarify if style="visibility:hidden" is correct.

    I went of the big code block and created a simple page with a label inside a non server side div and a  button. I set the div style="visibility:hidden" .

    I get the label on a page load and a button click.

    So even when the label is hidden I get it on server side.

    Is this an expected behavior?

    Thanks.

    Friday, March 1, 2019 8:15 PM
  • User475983607 posted

    Hi.

    I'm getting the code but I want to clarify if style="visibility:hidden" is correct.

    I went of the big code block and created a simple page with a label inside a non server side div and a  button. I set the div style="visibility:hidden" .

    I get the label on a page load and a button click.

    So even when the label is hidden I get it on server side.

    Is this an expected behavior?

    Thanks.

    I'm confused.  I explained exactly what to do.  

    style="display:none"

    Yet you ignored this advice.  You also ignored the advice about posting the actual code causing the issue.

    I'm not sure what we can do for you considering that testing this takes a few seconds.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="WebFormsDemo._default" %>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link href="content/bootstrap.css" rel="stylesheet" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" type="text/javascript"></script>
    </head>
    <body>
        <form runat="server">
            <div style="display:none">
                <asp:Label ID="srcDisplayNone" runat="server" Text="Hello from display:none"></asp:Label>
            </div>
            <div style="visibility:hidden">
                <asp:Label ID="srcVisibilityHidden" runat="server" Text="Hello from visibility:hidden"></asp:Label>
            </div>
            <div>
                <asp:Label ID="DisplayNone" runat="server" Text=""></asp:Label>
            </div>
            <div>
                <asp:Label ID="VisibilityHidden" runat="server" Text=""></asp:Label>
            </div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </form>
    </body>
    </html>
    
    
    
    
    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.UI;
    
    namespace WebFormsDemo
    {
        public partial class _default : System.Web.UI.Page
        {
    
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                DisplayNone.Text = srcDisplayNone.Text;
                VisibilityHidden.Text = srcVisibilityHidden.Text;
            }
        }
    }

    Friday, March 1, 2019 8:36 PM
  • User-2146352328 posted

    It's not that complicated

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="visibility:hidden">
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        </form>
    </body>
    </html>
    

    LAbel will always be caught on server code even if I use yle="visibility:hidden"

    Friday, March 1, 2019 9:03 PM
  • User475983607 posted

    sapator

    It's not that complicated

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div style="visibility:hidden">
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        </form>
    </body>
    </html>

    LAbel will always be caught on server code even if I use yle="visibility:hidden"

    Correct, and the expected behavior.  Can you explain the problem you are trying to solve?

    Perhaps you want to disable ViewState?

        <form runat="server">
            <div style="display:block">
                <asp:Label ID="srcDisplayNone" runat="server" EnableViewState="false"></asp:Label>
            </div>
            <div style="visibility:inherit">
                <asp:Label ID="srcVisibilityHidden" runat="server" EnableViewState="false"></asp:Label>
            </div>
            <div>
                <asp:Label ID="DisplayNone" runat="server"></asp:Label>
            </div>
            <div>
                <asp:Label ID="VisibilityHidden" runat="server"></asp:Label>
            </div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </form>
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    srcDisplayNone.Text = "Hello from display:none";
                    srcVisibilityHidden.Text = "Hello from visibility:hidden";
                }
            }
    
            protected void Button1_Click(object sender, EventArgs e)
            {
                DisplayNone.Text = srcDisplayNone.Text;
                VisibilityHidden.Text = srcVisibilityHidden.Text;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2019 9:21 PM
  • User-943250815 posted

    sapator,

    You can hide your DIV using visibility: hidden or display:none, the only difference between both is when you use visibility the hidden element take space on page
    https://www.w3schools.com/cssref/pr_class_visibility.asp

    You can set any CSS on your DIV, controls inside will be rendered on page anyway, any CSS definition apply only on how Browser will render page, but not page contents

    On the other hand from code-behind you can set your label visible or not, in this case you defining if your label should be part of page or not

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 1, 2019 11:00 PM
  • User-2146352328 posted

    Thanks both.

    I'm was a few years out of server side asp.net so I'm missing some basic stuff.

    Friday, March 1, 2019 11:39 PM