0

我在 aspx 页面上的工具栏按钮有问题。我用来仅在协议页面中再渲染 2 个按钮的方法如下。我仅在协议页面上的工具栏上呈现 2 个按钮(活动,存档)。但是,当这本书被归档时,我需要隐藏它们。我试图通过检查 if (Entity.IsArchived == 0) 来做到这一点,但它在界面中给了我以下错误:

错误详情

你调用的对象是空的。

该方法的代码是:

protected override void OnPrepareButtons(SortedList<string, ImageButton> buttons)
{
        // Activate button
        ImageButton img = new ImageButton();
        img.ID = "btnActivate";
        img.AlternateText = "Activate";
        img.Command += new CommandEventHandler(btnActivate_Click);
        img.CommandName = "Activate";
        img.ImageUrl = "~/Content/images/png/apply.png";
        img.Width = Unit.Pixel(25);
        img.Height = Unit.Pixel(25);
        img.ToolTip = "Activate";
        buttons.Add("Activate", img);

        // Archive button
        img = new ImageButton();
        img.ID = "btnArchive";
        img.AlternateText = "Archive";
        img.Command += new CommandEventHandler(btnArchive_Click);
        img.CommandName = "Archive";
        img.ImageUrl = "~/Content/images/png/lock.png";
        img.Width = Unit.Pixel(25);
        img.Height = Unit.Pixel(25);
        img.ToolTip = "Archive";
        buttons.Add("Archive", img);

        base.OnPrepareButtons(buttons);

}

知道如何处理这种情况吗?

4

1 回答 1

0

您收到问题中提到的错误,因为当您添加该行以检查 if 时Entity.IsArchived,您将抛出一个空异常。您需要调试设置实体的位置,以查看为什么在尝试使用它时它为空。

一旦您解决了 Entity 为空的原因,您将能够成功检查IsArchived并根据需要隐藏图像。

这不是解决方案。如果您不确定调试:如果您添加了这个:

if(Entity == null)
    MessageBox.Show("Entity is null!!");

你会看到消息框:)

于 2013-05-31T13:32:17.830 回答