我有一个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";
感谢。
答案 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" />
快乐编程!