代码之家  ›  专栏  ›  技术社区  ›  Mark Allison

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

  •  0
  • Mark Allison  · 技术社区  · 14 年前

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

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

    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 回复  |  直到 14 年前
        1
  •  1
  •   Brian Minnich    14 年前

    让我们放弃覆盖按钮字段的想法,使用一个简单的模板字段。它将正确处理post back并引发GridView的(RowCommand&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
    
        }
    }
    
        2
  •  1
  •   Scott Mitchell    14 年前

    马克,我觉得你的定制课非常非常接近。对于 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" /> 
    

    编程愉快!

    推荐文章