2010-03-09 132 views
1

在我的aspx页面我有一个tr默认设置为visible="false"。但是在选择的下拉列表中,我使它成为visible="true"。在表单提交中,我正在验证tr内的控件,但无法使用JavaScript查找tr是否可见。JavaScript的style.visibility似乎并没有工作

我的aspx:

<tr id="MeasurementTr" runat="server" visible="false"> 
    <td> 
     &nbsp;</td> 
    <td class="table_label"> 
     Measurement</td> 
    <td> 
     &nbsp;</td> 
    <td> 
     <asp:DropDownList ID="DlMeasurement" runat="server"> 
     </asp:DropDownList> 
    </td> 
    <td> 
     &nbsp;</td> 
</tr> 

和我的JavaScript代码,

alert(document.getElementById("ctl00_ContentPlaceHolder1_MeasurementTr").style.visibility); 
if (document.getElementById("ctl00_ContentPlaceHolder1_MeasurementTr").style.visibility=="visible"){ 
    if (document.getElementById("ctl00_ContentPlaceHolder1_DlMeasurement").selectedIndex == 0) { 
     document.getElementById("ctl00_ContentPlaceHolder1_ErrorMsg").innerHTML = "Please Select Your Measurement"; 
     document.getElementById("ctl00_ContentPlaceHolder1_DlMeasurement").focus(); 
     return false; 
    } 
} 

但我的警报显示什么。它没有显示nullundefined

回答

1

asp.net的visible属性不改变CSS visibility属性..当它是真实的,asp.net将不会呈现在所有客户端上的元素,所以你不能访问它..

使用带有 visibility:hidden display:none代替一类..

[UDPATE]

改变的建议,display:none AF ter cheeso的评论,因为visibility:hidden将保留该元素占据的空间,而display:none在呈现的页面中不占用空间..这很可能是您所需要的...

+0

还要考虑CSS显示:无风格。这与可见度不同,但可能是你想要的。 http://www.devx.com/tips/tip/13638 – Cheeso 2010-03-09 12:00:05

+0

@cheeso,非常真实..我将这个添加到答案.. – 2010-03-09 12:05:24

+0

是的,在CSS中,'display:none'比'visibility:隐藏“到ASPNET的”可见“属性。 – Cheeso 2010-03-09 12:24:39

3

visible property可以取值hidden,visiblecollapse

true and false are invalid CSS。

.style.*属性表示直列 CSS(如在style属性指定)。如果您使用样式表设置值,那么将不会反映在元素上的.style.*中。

作为一个经验法则,你通常最好修改.className

+2

'visible'属性是一个asp.net属性... CSS一个是'visibility' .. – 2010-03-09 11:44:20

+0

@David在我选择的索引中改变了事件我给了 'protected void DlMeasurement_SelectedIndexChanged(object sender,EventArgs e) { MeasurementTr.Visible = true;' – 2010-03-09 11:45:32

+0

@Gaby如何在javascript中获得asp.net属性... – 2010-03-09 11:47:44

-1

ASP控件不可见“服务器端”不在“客户端”上呈现,因此HTML中不存在MeasurementTr元素。使用视图源(或其他技术)来查看浏览器呈现的HTML。