开发者

Replace buttoncolumn "Delete" with a Image Button in Gridview

This is my Gridview code

<asp:DataGrid id="dg" runat="server" ondeletecommand="Delete_Item" >

        <columns>

        <asp:buttoncolumn buttontype="LinkButton" commandname="Delete" text="Remove" />

        </columns>
            <HeaderStyle BackColor="#95C736" ForeColor="White开发者_如何转开发" Font-Bold="True" />
        </asp:DataGrid>

I want to replace my buttoncolumn with an image, what must I do to my GridView?


I would use a template column for this:

<asp:DataGrid ID="DataGrid1" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:ImageButton ID="btnDelete" runat="server" ImageUrl="/images/delete.png" CommandName="Delete" />
            </ItemTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid> 


Replace the Button Column with a TemplateColumn with allows you to put standard asp controls inside. Then you can handle the Datagrid_ItemCommand event normally.

    <asp:DataGrid ID="dgTest" runat="server">
        <Columns>
            <asp:TemplateColumn>
                <ItemTemplate>
                    <asp:ImageButton ID="ibtnDelete" runat="server" CommandName="cmdDelete"
                        ImageUrl="~/images/delete.png" />
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>

The ItemCommand handler would be something like:

Protected Sub dgTest_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgTest.ItemCommand
    If (e.CommandName = "cmdDelete") Then
        Response.Write("the command argument was :" & e.CommandArgument)
    End If
End Sub

The only other thing you would need to do is bind some data to the image button for a command argument. I usually do something like:

Protected Sub dgTest_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgTest.ItemDataBound
    If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim di As FAQTable = e.Item.DataItem
        DirectCast(e.Item.FindControl("ibtn"), ImageButton).CommandArgument = di.FAQID
    End If
End Sub

You could also use an asp:image control with an asp:Hyperlink control to get the same results.


One thing I just did that worked awesome is to put encoded html into the text. Text="&lt;img src='images/btn-find.png' class='ttip' alt='View Details' /&gt;" This let me know only put in the img src, but also specify a class and an alt tag.

All you need to do is use single tick marks and encode your <> with gt and lt.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜