2016-12-02 74 views
1

如何禁用/隐藏项目模板内部的按钮?我想在加载时隐藏保存按钮,然后显示它并在单击编辑按钮时隐藏编辑按钮。在项目模板中禁用图像按钮

模板:

<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 
    } 
+0

你使用的是什么版本的asp.net? 也确保你清楚启用和可见 – DaniDev

回答

0

使用事件的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 
} 
+0

之间的差异,但我如何在相同的buttonclick更新ImageButtonEdit呢? –

0

您需要检索的ImageButton如下,因为它是居住在GridView

private void ImageButtonUpdate_Click(object sender, EventArgs e) 
{ 
    //Get the ImageButton that raised the event 
    ImageButton btn = (ImageButton)sender; 
    btn .Enabled = false; 
} 

编辑

步骤1

Update提供相同onClick事件和Edit

OnClick="ImageButtonUpdate_Click" 

步骤2

使用CommandName属性

 ImageButton btn = (ImageButton)sender; 
     if(btn.CommandName =="Update") 
     { 
      btn.Enabled = false; 
     } 
     if(btn.CommandName =="Edit") 
     { 
      btn.Enabled = false; 
     } 
+0

但我如何在同一个buttonclick更新ImageButtonEdit呢? –

+0

@MathiasRønnowNørtoft您的'btn'将具有'commandname'属性,您可以更新'imagebuttonedit' – Webruster

+0

请注意让我举个例子吗?我不知道我会如何做到这一点。 –

相关问题