简体   繁体   中英

How can I add programmatically a more complex column in an 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" />

That I must insert programmatically in my code using C#. Can someone please help? I don't know how to insert ImageButton in a TemplateField or even insert all this data at once.

Rather than dynamically inserting it, I'd recommend setting Visible="False" on it, and in code set it to true when you want to show it. It will be much easier to manage then; for instance, in RowDataBound event handler, you can do:

GridView_RowDataBound(..)
{
    var deleteButton = e.Row[0].FindControl("imbDelete");
    deleteButton.Visible = (some condition);
}

i've ever do that with an asp button and using a CssClass for the picture.

 <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;
}

if you want you can change the picture on hover like that :

.buttonDelete:hover
{
background:url(../../images/buttonDelete-hover.png) no-repeat;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM