Reputation: 803
How do i disable/hide a button inside that is inside a itemtemplate? i want to hide the save button on load, and then show it and hide the edit button when the edit button is clicked.
The template:
<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>
Behind:
private void ImageButtonUpdate_Click(object sender, EventArgs e)
{
ImageButtonUpdate.Enabled = false; // not working, dosnt find the button
}
Upvotes: 1
Views: 548
Reputation: 8271
You Need to retrieve the ImageButton as follows as it is residing in the GridView
private void ImageButtonUpdate_Click(object sender, EventArgs e)
{
//Get the ImageButton that raised the event
ImageButton btn = (ImageButton )sender;
btn .Enabled = false;
}
Edit
Step 1
Provide same Onclick event for Update
and Edit
i.e
OnClick="ImageButtonUpdate_Click"
Step 2
Use CommandName
Attribute
ImageButton btn = (ImageButton )sender;
if(btn.CommandName =="Update")
{
btn.Enabled = false;
}
if(btn.CommandName =="Edit")
{
btn.Enabled = false;
}
Upvotes: 1
Reputation: 63065
use the sender
of the event
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
}
Upvotes: 1