Hide Imagebutton from ASP.NET onclick



我试图隐藏/显示ASP.NET模板中的编辑/保存按钮。因此,当未选择行时,我也希望"编辑"按钮显示,然后在单击上隐藏它,然后使"保存"按钮可见。
如何访问和更新属性?
我尝试的解决方案只给了我"Null"

我拥有的:

<ItemTemplate>
     <asp:ImageButton ID="ImageButtonEdit" runat="server" CommandName="Edit" ImageUrl="loginstyling/images/Edit.png"/>
     <asp:ImageButton ID="ImageButtonUpdate" runat="server" CommandName="Update" ImageUrl="loginstyling/images/Save.png" OnClick="ImageButtonUpdate_Click" Visible="true"/>
     <asp:ImageButton ID="ImageButtonDelete" runat="server" CommandName="Delete" ImageUrl="loginstyling/images/Remove.png" visible="false" />
</ItemTemplate>

我在背后尝试了什么:

protected void ImageButtonUpdate_Click(object sender, ImageClickEventArgs e)
{
    ImageButton test = (ImageButton)GridView1.FindControl("ImageButtonUpdate");
    test.Attributes.Add("Visible", "False");
}

如果有效,请尝试:

    test.Visible = false;

尝试使用rowdataboundevent进行gridview,如果有效:

protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ImageButton test = (ImageButton)e.Row.FindControl("ImageButtonUpdate");
        test.Visible = false;
    }
}

您确定.FindControl("ImageButtonUpdate");返回有效对象吗?

由于它返回null检查另一个帖子findcontrol()返回null,因此似乎与您遇到的问题相同。

最新更新