如何在c#中为按钮定义添加工具提示
本文关键字:按钮 定义 添加 工具提示 | 更新日期: 2023-09-27 18:15:47
我用c#在后面的for循环中定义了按钮
protected void GenTable()
{
for (int r = 0; r < 100; r++)
{
var buttonField = new ButtonField
{
ButtonType = ButtonType.Button,
Text = "Model",
CommandName = "Display",
};
Model.Columns.Add(buttonField);
break;
}
}
如何为这些按钮添加工具提示?我尝试将工具提示或标题添加到for循环中,但不工作。有人能帮我修一下吗?由于
在System.Web.UI.WebControls.ButtonField
上没有任何你想做的选择。
然而,有一个可能的解决方法。您可以将其Text
属性设置为HTML
属性,例如
<asp:ButtonField Text="<span title='My beautiful button'>Model</span>" />
由于您正在通过c#设置Text
,因此它们似乎无法转义<
, >
字符。
RowDataBound
事件。例如:给定一个这样的GridView:
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" runat="server">
第一列如下:
<asp:ButtonField Text="Model" />
您可以使用下面的事件ItemDataBound
事件:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
(((System.Web.UI.WebControls.WebControl)e.Row.Cells[0].Controls[0])).ToolTip = "My beautiful button";
}
}
注意:将Cells[0]
更改为您想要的列的索引,因此,如果您创建了100列,并希望显示100个工具提示(就像您的代码一样),您必须在那里循环并更改为Cells[i]
。