如何在ASP.Net GridView中使用AutoGenerateEditButton的图像而不是文本

Bra*_*uce 4 asp.net gridview

我正在使用AutoGenerateEditButton以及Delete和Select.

我想使用图像而不是文本作为链接.

我该怎么做呢?

我不想手动创建命令列,因为AutoGenerate属性在我正在处理的大型项目中使用.

Kel*_*sey 5

最简单的方法是自己处理.以下是使用an ImageButton替换编辑命令按钮的快速示例:

<asp:GridView ID="yourGrid" runat="server" OnRowEditing="yourGrid_RowEditing">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ID="yourEditButton" runat="server"
                    CommandName="Edit" ImageUrl="edit.jpg" />
            </ItemTemplate>
            <EditItemTemplate>
                <!-- your edit controls here -->
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)

现在为后面的代码:

protected void yourGrid_RowEditing(object sender, GridViewEditEventArgs e)
{
    // You could just do yourGrid and ignore casting the sender but this 
    // makes the code generic for reuse.
    GridView grid = (GridView)sender;   
    grid.EditIndex = e.NewEditIndex;
    BindData(); // need to rebind once the edit index is set.
}
Run Code Online (Sandbox Code Playgroud)

这几乎取代了自动生成的编辑按钮ImageButton.通过设置CommandName编辑,它将触发与自动生成的编辑按钮完全相同的事件.这也适用于删除,更新等...