禁用itemtemplate中的imagebutton

时间:2016-12-02 16:58:09

标签: c# asp.net

如何禁用/隐藏itemtemplate内的按钮?我想在加载时隐藏保存按钮,然后在单击编辑按钮时显示并隐藏编辑按钮。

模板:

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

落后:

        private void ImageButtonUpdate_Click(object sender, EventArgs e)
    {
        ImageButtonUpdate.Enabled = false; // not working, dosnt find the button
    }

1 个答案:

答案 0 :(得分:1)

使用活动的sender

protected void ImageButtonUpdate_Click(object sender, EventArgs e){
    ImageButton btnupdate= sender as ImageButton;
    btnupdate.Enabled = false;
    //if you need to get other controls in the same row
    GridViewRow row = (GridViewRow)btnupdate.NamingContainer;
    var btnedit=  (ImageButton)row.FindControl("ImageButtonEdit");
    btnedit.Enabled = false; // do enable or disable as you need
}