可以用GridView中的图像替换ButtonField吗?

时间:2010-11-19 19:58:10

标签: c# asp.net buttonfield

我有一个GridView,它有一个删除链接来删除记录。我创建了一个DeleteButtonField类,但我想用图像(图标)替换文本。这可能吗?这是我的GridView:

<asp:GridView 
    ID="GridView1" 
    runat="server"
    <.. removed dome properties ..> 
    >
    <Columns>
    <CustomControls:DeleteButtonField ConfirmText="Delete this record?" /> 
    <.. other columns ..>
    </Columns>
</asp:GridView>

这是我的DeleteButtonField类:

using System;
using System.Web.UI.WebControls;

namespace CustomControls
{
    public class DeleteButtonField : ButtonField
    {
        private string _confirmText = "Delete this record?";

        public string ConfirmText
        {
            get { return _confirmText; }
            set { _confirmText = value; }
        }

        public DeleteButtonField()
        {
            this.CommandName = "Delete";
            this.Text = "Delete";
            this.ImageUrl = "App_GlobalResources/Del.png";   // doesn't work
        }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);
            if (cellType == DataControlCellType.DataCell)
            {
                WebControl button = (WebControl)cell.Controls[0];
                button.Attributes["onclick"] = String.Format("return confirm('{0}');", _confirmText);
            }
        }
    }
}

这可能吗?如您所见,我将以下代码添加到我的DeleteButtonField.cs类中,但它没有任何效果:this.ImageUrl = "App_GlobalResources/Del.png";

感谢。

2 个答案:

答案 0 :(得分:1)

让我们想象一下覆盖按钮字段并使用简单的模板字段。它将正确处理post backs并引发GridView的(RowCommand&amp; RowDeleting)事件。希望这可以帮助! =)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace MyExample.Web
{
    public class MyDeleteButtonField : TemplateField
    {
        #region Properties

        private string _ConfirmText = "Delete Me?";
        public string ConfirmText
        {
            get { return _ConfirmText; }
            set { _ConfirmText = value; }
        }

        private string _ImageUrl = "~/Assets/Images/Buttons/flip.png";
        public string ImageUrl
        {
            get { return _ImageUrl; }
            set { _ImageUrl = value; }
        }

        #endregion

        #region Methods

        public override bool Initialize(bool sortingEnabled, System.Web.UI.Control control)
        {
            base.ItemTemplate = new MyTemplate(this.ConfirmText, this.ImageUrl);
            return base.Initialize(sortingEnabled, control);
        }

        #endregion

        #region Template

        public class MyTemplate : ITemplate
        {
            private string _ConfirmText;
            private string _ImageUrl;

            public MyTemplate(string confirmText, string imageUrl)
            {
                _ConfirmText = confirmText;
                _ImageUrl = imageUrl;
            }

            void ITemplate.InstantiateIn(Control container)
            {
                ImageButton bt = new ImageButton();
                bt.CommandName = "Delete";
                bt.ImageUrl = _ImageUrl;
                bt.ImageAlign = ImageAlign.AbsMiddle;
                bt.AlternateText = "Delete Me";
                bt.OnClientClick = String.Format("return confirm('{0}');", _ConfirmText);
                container.Controls.Add(bt);

            }
        }

        #endregion

    }
}

答案 1 :(得分:1)

马克,我认为你的定制课非常非常接近。要使ButtonField类显示图像,您需要指定ImageUrl属性ButtonType属性设置为ButtonType.Image

尝试将DeleteButtonField类的构造函数更新为:

public DeleteButtonField()
{
    this.CommandName = "Delete";
    this.Text = "Delete";
    this.ImageUrl = "App_GlobalResources/Del.png";
    this.ButtonType = ButtonType.Button;
}

或者,您可以通过.aspx页面中的声明性语法指定这些设置:

<CustomControls:DeleteButtonField ConfirmText="Delete this record?" ImageUrl='...' ButtonType="Image" /> 

快乐编程!