5

I have a asp:Panel element on my page. I'm able to set its visibility in code behind. But I need to also hide it through javascipt.

My panel is defined as following:

     <asp:Panel ID="pnlUpdateDisplay" runat="server" Visible="false" style="width:500px; border-width: thick;">
        <table style="width:300px;">
            <tr>
                <td valign="middle" style="width:120px" align="center">
                <asp:Label ID="lblUpdateMessage" runat="server" style="position:absolute; left: 120px; top: 120px;"></asp:Label>
                </td>
            </tr>
        </table>      
    </asp:Panel>

When I do this:

   var panel = document.getElementById('pnlUpdateDisplay');
   panel.style.visibility = 'hidden';
   panel.style.display='none';

There is an error saying: "Error: Unable to get value of the property 'style': object is null or undefined"

Any suggestions?

3
  • Paste your generated html in your question Commented Mar 24, 2014 at 16:39
  • Actually, for some reason, there is not generated html for panel and other elements on a page but I'm able to see them on a page Commented Mar 24, 2014 at 16:42
  • 1
    @eugene.it, just noticed that the panel has Visible=false. ASP.NET would not render it at all Commented Mar 24, 2014 at 16:46

2 Answers 2

14

Setting Visible=false to the server control in ascx/aspx mark up or in a code behind prevent the control being rendered in DOM. So you will not find them in DOM and it won't be accessible to JavaScript

Better remove Visible="false" set in the panel and add style display:none.

If you want to make it in code behind follow this code

pnlUpdateDisplay.Style.Add(HtmlTextWriterStyle.Display,"none");

Then use

$('#<%=pnlUpdateDisplay.ClientID %>').toggle()
Sign up to request clarification or add additional context in comments.

Comments

2

You can use .toggle() to toggle between show and hide:

$('#pnlUpdateDisplay').toggle();

If you want to hide it only then use .hide()

$('#pnlUpdateDisplay').hide();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.