Asp.net 如何添加Commandfield ButtonType作为图像的工具提示
我必须为我的gridview编辑图像按钮添加工具提示。我的代码是CommandfieldAsp.net 如何添加Commandfield ButtonType作为图像的工具提示,asp.net,css,Asp.net,Css,我必须为我的gridview编辑图像按钮添加工具提示。我的代码是Commandfield <asp:CommandField ButtonType="Image" HeaderStyle-HorizontalAlign="center" EditText="Edit" UpdateText="Edit" ShowEditButton="True" ItemStyle-VerticalAlign="Top" EditImageUrl="~/I
<asp:CommandField ButtonType="Image"
HeaderStyle-HorizontalAlign="center"
EditText="Edit" UpdateText="Edit"
ShowEditButton="True"
ItemStyle-VerticalAlign="Top"
EditImageUrl="~/Images/edit.png"
UpdateImageUrl ="~/Images/Save.png" CancelImageUrl ="~/Images/cancel.png"
ItemStyle-Width="15px" ControlStyle-Width="15px">
</asp:CommandField>
是否可以在gridview commandfield按钮类型中添加工具提示作为图像?以下代码运行良好:
protected void gvResourceEditor_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[4].ToolTip = "Edit Resource Details";
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState.ToString() == "Alternate, Edit")
{
int i = 0;
foreach (TableCell cell in e.Row.Cells)
{
if (e.Row.Cells.GetCellIndex(cell) == 4)
{
((System.Web.UI.WebControls.ImageButton)(e.Row.Cells[4].Controls[0])).ToolTip = "Update Resource Details";
((System.Web.UI.LiteralControl)(e.Row.Cells[4].Controls[1])).Text = " ";
((System.Web.UI.WebControls.ImageButton)(e.Row.Cells[4].Controls[2])).ToolTip = "Close Resource Details";
}
i++;
}
}
}
}
catch (Exception _e)
{
}
}
或者你可以
标记的alt
atribute:
title
属性:当然,您可能需要将javascript添加到网格中。此脚本将使用
type=image
设置所有input
标记的title属性,我没有尝试Sekaran
和Show
建议的解决方案,并且采取了一种稍微不同的方法,因为我不愿意依赖JavaScript
;我觉得这可能值得分享
在他的解决方案中提到,编辑按钮的EditText
属性将被呈现为图像的alt
属性;这意味着它也应该存储为.NET对应项的AlternateText。
就我而言;我在我的命令字段
中有编辑
、更新
、删除
和取消
按钮,我想对所有按钮执行相同的操作
因此(本地化)文本位于按钮的AlternateText
属性中;我正在循环遍历每个按钮,并将此值放入工具提示
属性中。
这是我的密码
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (Control ctrl in e.Row.Cells[2].Controls)
{
if (ctrl.GetType().BaseType == typeof(ImageButton))
((ImageButton)ctrl).ToolTip = ((ImageButton)ctrl).AlternateText;
}
}
}
我在我的代码中硬编码了单元Id“2”,因为它是众所周知的。
每个图像按钮控件都是无法访问的
System.Web.UI.WebControls.DataControlImageButton
类型。因此,我使用了它的基类型,它应该是ImageButton
在RowDataBound期间,@show答案的RegisterStartupScript部分不需要运行。在网格数据绑定过程中,它将为每一行执行完全相同的操作。您可以将这两行放到页面的OnPreRender事件中,它将具有相同的效果,但在服务器上执行代码时只发生一次
protected void gvResourceEditor_RowDataBound(object sender, GridViewRowEventArgs e) {
string strScript = "$('#" + gvResourceEditor.ClientID + " input[type=image]').each(function(key, el) {el.title=el.alt;});"
Page.ClientScript.RegisterStartupScript(this.GetType(), "SetEditButtonTitle", strScript, true);
}
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (Control ctrl in e.Row.Cells[2].Controls)
{
if (ctrl.GetType().BaseType == typeof(ImageButton))
((ImageButton)ctrl).ToolTip = ((ImageButton)ctrl).AlternateText;
}
}
}