Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从工具栏隐藏按钮?_C#_Asp.net_Button_Hide - Fatal编程技术网

C# 如何从工具栏隐藏按钮?

C# 如何从工具栏隐藏按钮?,c#,asp.net,button,hide,C#,Asp.net,Button,Hide,aspx页面上的工具栏按钮有问题。 我只在协议页面中渲染另外两个按钮的方法如下。 我只在协议页面的工具栏上呈现2个按钮(活动、存档)。但是,我需要在书归档时隐藏它们。我试图通过检查(Entity.IsArchived==0)来执行此操作,但它在接口中给了我以下错误: 错误详细信息 对象引用未设置为对象的实例 该方法的代码为: protected override void OnPrepareButtons(SortedList<string, ImageButton> buttons

aspx页面上的工具栏按钮有问题。 我只在协议页面中渲染另外两个按钮的方法如下。 我只在协议页面的工具栏上呈现2个按钮(活动、存档)。但是,我需要在书归档时隐藏它们。我试图通过检查(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);

}
protected override void OnPrepareButtons(分类列表按钮)
{
//激活按钮
ImageButton img=新建ImageButton();
img.ID=“btnActivate”;
img.AlternateText=“激活”;
img.Command+=新建CommandEventHandler(btnActivate\u单击);
img.CommandName=“激活”;
img.ImageUrl=“~/Content/images/png/apply.png”;
图像宽度=单位像素(25);
img.高度=单位像素(25);
img.ToolTip=“激活”;
按钮。添加(“激活”,img);
//存档按钮
img=新图像按钮();
img.ID=“btnArchive”;
img.AlternateText=“存档”;
img.Command+=newcommandeventhandler(btnArchive\u单击);
img.CommandName=“存档”;
img.ImageUrl=“~/Content/images/png/lock.png”;
图像宽度=单位像素(25);
img.高度=单位像素(25);
img.ToolTip=“存档”;
按钮。添加(“存档”,img);
基本准备按钮(按钮);
}

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

您遇到了问题中提到的错误,因为当您添加行以检查是否
Entity.isarchive
时,您抛出了一个空异常。您需要调试实体被设置的位置,以了解在您尝试使用它时为什么它为空

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

这不是一个解决办法。如果您对调试不确定:如果您添加了以下内容:

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

您将看到消息框:)

此错误是否来自此代码(哪一行)?或者它发生在base.OnPrepareButtons中?我在最后一行之后添加了以下代码行:if(Entity.IsArchived==1){img.Visible=false;},但仍然生成错误:(@jbl,如果我让代码保持原样,不添加任何内容,它会重建ok,不会生成错误,但它不会隐藏按钮,即使书籍可能已存档。如果我添加代码行,它会生成我告诉过你的错误。我想这意味着,当代码运行时,页面/控件的实体属性尚未加载。你什么时候开始加载实体?此代码何时运行?此代码在呈现页面时运行,因此它会显示页面上的按钮,用户可以激活和归档书籍。