Show/Hide elements in ASP.Net without postback.

Is there anyway to show/hide elements on a web form without doing a postback?  Thanks!
EDW_GideonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
To hide :
document.getElementById("<%= TextBox1.ClientID %>").style.display = "none";
To show :
document.getElementById("<%= TextBox1.ClientID %>").style.display = "none";

Be sure to use CSS to hide your element and not the Visible propertie
So instead :
<asp:Textbox ID="TextBox1" Visible="False"
use :
<asp:Textbox ID="TextBox1" Style="display:none;"
0
Hans LangerCommented:
ASP.NET is compiled and executed in the server side, that why it need to do post back (Sync or Async).
If you want to hide an HTML element without using ASP.net, then you can use the <script> tags in your HTML code to add a javascript code that will be executed when the client receive the page:
<html>
<head>
<script>
window.onload = function(){ // this function will be executed after all the elements of the pages are loaded and ready.
    //ADD here to hide the elements
   document.getElementById('fieldtohide').style.display = "none";
   document.getElementById('fieldtoshow').style.display = "visible";
}

function toggleElementVisibility(element){
  element.style.display = (element.style.display = 'none') ? 'visible' : 'none'
}

</script>
</head>
<body>
.....
</body>
</html>

Open in new window



you can use "toggleElementVisibility" function to toggle visible/hide an element.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sammySeltzerCommented:
To show:
document.getElementById("<%= TextBox1.ClientID %>").style.display = "block";

This is probably a case of copy and paste.
0
Hans LangerCommented:
You are right. it's .style ='block" instead of .style='visible', i don't know what I was thinking on :P
0
Tapan PattanaikSenior EngineerCommented:
Hi EDW_Gideon,

This is one of the example to  hide /show divs in asp.net.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function hideDiv() {
            if (document.getElementById) {
                document.getElementById('div').style.display = 'none';
            }
        }
        function showDiv() {
            if (document.getElementById) {
                document.getElementById('div').style.display = 'block';
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>         
                <asp:Button id="btnHide" runat="server" Text="Hide" ClientIDMode="Static" OnClientClick="javascript:hideDiv(); return false;"></asp:Button>
                <asp:Button id="btnShow" runat="server" Text="Show" ClientIDMode="Static" OnClientClick="javascript:showDiv(); return false;"></asp:Button>
                <div id="div">show/hide elements on a web form without doing a postback</div>
        </div>
    </form>
</body>
</html>

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.