1

The desired effect is that on page load the label does not display but when the user clicks the check box the label displays. This small code sample is just a sample to illustrate the issue. The code will always return an object reference exception from javascript when the Visible property of the label is set to false. If that line is commented out, it will execute correctly with no object reference exceptions but the label should be hidden at page load. This application does use master pages which is why were are passing the ClientIDs to the javascript Toggle function.

protected void Page_Load(object sender, EventArgs e)
{
    this.chkSelect.Attributes.Add("onClick", "Toggle('" + this.lblAdd.ClientID + "', '" + this.chkSelect.ClientID + "')");
    this.lblAdd.Visible = false;
}

<script type="text/javascript">
    function Toggle(lblAdd, chk) {
        var ctrlAdd = document.getElementById(lblAdd);
        var ctrlChk = document.getElementById(chk);

        if (ctrlChk.checked == true) {
            ctrlAdd.style.display = 'inline';
        }
        else {
            ctrlAdd.style.display = 'none';
        }
    }
</script>


    <asp:Label ID="lblAdd" runat="server" Text="Add" Font-Size="8pt" ForeColor="Blue"> </asp:Label>
    <asp:CheckBox ID="chkSelect" runat="server" Text="Check Box1" /><br />

How can we hide that label in Page_Load so we don't get object reference errors from Internet Explorer?

Thanks...

4

1 回答 1

5

如果设置为 false,该Visible属性不会呈现 HTML,这就是您获得空引用的原因(换句话说,ASP.NETVisible与 CSS 属性无关display- 它实际上切换元素是否在 HTML 代码中呈现.)。而是在页面加载时分配一个定义的 CSS 类display: none,然后在单击时使用 javascript 删除该类

于 2013-05-14T21:10:36.060 回答