How to add program code to a more complex column in gridview
I have the following columg:
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imbDelete" runat="server" ImageUrl="~/images/icon_Delete.png"
OnClientClick="return confirm('Delete attachment?');" CommandName="Delete"
CommandArgument='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Path" HeaderText="Path" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />
What should I programmatically insert into my code using C #. Can someone help? I don’t know how to embed ImageButton in TemplateField or even paste all this data at once.
Instead of dynamically inserting it, I would recommend setting it on it Visible="False", and in the code it set to true when you want to show it. Then it will be much easier to manage; for example, in the RowDataBound event handler, you can:
GridView_RowDataBound(..)
{
var deleteButton = e.Row[0].FindControl("imbDelete");
deleteButton.Visible = (some condition);
}
I have ever done this with an asp button and using a CssClass for the image.
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button ID="imbDelete" runat="server" CssClass="buttonDelete"
OnClientClick="return confirm('Delete attachment?');" CommandName="Delete"
CommandArgument='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Path" HeaderText="Path" ReadOnly="True" Visible="False" />
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />
then in your CssClass:
.buttonDelete
{
border:none;
background:url(../../images/buttonDelete.png) no-repeat;
}
, :
.buttonDelete:hover
{
background:url(../../images/buttonDelete-hover.png) no-repeat;
}