加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:22:04 所属栏目:asp.Net 来源:网络整理
导读:我正在使用AutoGenerateEditButton以及Delete和Select. 我想使用图像而不是文本作为链接. 我该怎么做呢? 我不想手动创建命令列,因为AutoGenerate属性在我正在处理的大型项目中使用. 解决方法 最简单的方法是自己处理.以下是使用 ImageButton替换编辑命令按
我正在使用AutoGenerateEditButton以及Delete和Select.

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

我该怎么做呢?

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

解决方法

最简单的方法是自己处理.以下是使用 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>

现在为后面的代码:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读