如何在ASP.Net GridView中使用图像而不是文本作为AutoGenerateEditButton [英] How do I use images instead of text for AutoGenerateEditButton in ASP.Net GridView

查看:97
本文介绍了如何在ASP.Net GridView中使用图像而不是文本作为AutoGenerateEditButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是AutoGenerateEditButton以及Delete和Select。

I'm using AutoGenerateEditButton and the Delete and Select ones as well.

我想为链接使用图片而不是文本。

I'd like to use images instead of text for the links.

我该怎么做?

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

I don't want to manually create the command columns, since the AutoGenerate properties are used throughout a large project I'm working on.

推荐答案

子类GridView控件,并通过CreateChildControls方法

Subclass the GridView control and over ride the CreateChildControls method

    protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
    {
        // re-use the AutoGenerate...Button properties
        bool showDelete = AutoGenerateDeleteButton;
        bool showEdit = AutoGenerateEditButton;
        bool showSelect = AutoGenerateSelectButton;

        // turn them all off, we'll be creating our own
        AutoGenerateDeleteButton = false;
        AutoGenerateEditButton = false;
        AutoGenerateSelectButton = false;

        // hide the column if it already exists
        if (Columns[0].GetType() == typeof(CommandField))
        {
            Columns.RemoveAt(0);
        }

        // add the command column if necessary
        if (showDelete || showEdit || showSelect)
        {
            CommandField cmdField = new CommandField();
            cmdField.HeaderText = string.Empty;
            cmdField.ButtonType = ButtonType.Image;
            cmdField.ShowSelectButton = showSelect;
            cmdField.ShowEditButton = showEdit;
            cmdField.ShowDeleteButton = showDelete;
            cmdField.DeleteImageUrl = "~/images/delete.bmp";
            cmdField.EditImageUrl = "~/images/edit.bmp";
            cmdField.SelectImageUrl = "~/images/select.bmp";

            Columns.Insert(0, cmdField);
        }

        // this will show the grid even if there is no data
        int numRows = base.CreateChildControls(dataSource, dataBinding);

        //no data rows created, create empty table if enabled
        if (numRows == 0 && ShowWhenEmpty)
        {
            //create table
            Table table = new Table();
            table.ID = this.ID;

            //convert the exisiting columns into an array and initialize
            DataControlField[] fields = new DataControlField[this.Columns.Count];
            this.Columns.CopyTo(fields, 0);

            if (this.ShowHeader)
            {
                //create a new header row
                _headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

                this.InitializeRow(_headerRow2, fields);
                table.Rows.Add(_headerRow2);
            }

            //create the empty row
            GridViewRow emptyRow = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);

            TableCell cell = new TableCell();
            cell.ColumnSpan = this.Columns.Count;
            cell.Width = Unit.Percentage(100);
            if (!String.IsNullOrEmpty(EmptyDataText))
                cell.Controls.Add(new LiteralControl(EmptyDataText));

            if (this.EmptyDataTemplate != null)
                EmptyDataTemplate.InstantiateIn(cell);

            emptyRow.Cells.Add(cell);
            table.Rows.Add(emptyRow);

            if (this.ShowFooter)
            {
                //create footer row
                _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);

                this.InitializeRow(_footerRow2, fields);
                table.Rows.Add(_footerRow2);
            }

            this.Controls.Clear();
            this.Controls.Add(table);
        }

        // I wanted one place to set alternating color for all instances of this control
        base.AlternatingRowStyle.BackColor = System.Drawing.Color.LightBlue;

        // now that the controls have been created, it's safe to reset these to their original values.  They'll be needed if you bind data later
        AutoGenerateDeleteButton = showDelete;
        AutoGenerateEditButton = showEdit;
        AutoGenerateSelectButton = showSelect;

        return numRows;
    }

这篇关于如何在ASP.Net GridView中使用图像而不是文本作为AutoGenerateEditButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆