如何使用 C# 向按钮定义后面添加工具提示
How to add tooltips to button define behind with C#
我用 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]
.
我用 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]
.