2014-03-24 66 views
3

我的頁面上有一個asp:Panel元素。我可以在代碼背後設置其可見性。但我需要通過javascipt來隱藏它。如何使用javascript/jquery設置可見/隱藏的asp面板元素

panel定義如下:

 <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> 

當我這樣做:

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

有一個錯誤說:「錯誤:無法獲得屬性 '風格' 的價值:object is null or undefined「

有什麼建議嗎?

+0

在你的問題 – Turnip

+0

其實你生成html粘貼,出於某種原因,有沒有生成html面板和頁面上的其他元素,但我可以在頁面上看到它們 –

+1

@ eugene.it,只是注意到該面板具有'Visible = false'。 ASP.NET根本不會渲染它 – Andrei

回答

12

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

更好刪除Visible="false"集的面板並添加樣式display:none

如果你想讓它在後面的代碼遵循該代碼

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

然後使用

$('#<%=pnlUpdateDisplay.ClientID %>').toggle() 
2

您可以使用.toggle()顯示和隱藏之間切換:

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

如果你想隱藏它才用.hide()

$('#pnlUpdateDisplay').hide(); 
相關問題