C# 将工具提示设置为 <asp:Label> 或 <asp:Literal>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12470667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:22:30  来源:igfitidea点击:

C# setting a tooltip to a <asp:Label> or a <asp:Literal>

c#asp.net

提问by SF Developer

What's the best way to ADD a TOOLTIP to either a Label or a Literal from the Code Behind of a C# ASP.NET site ??

从 C# ASP.NET 站点的代码隐藏向标签或文字添加 TOOLTIP 的最佳方法是什么?

采纳答案by Habib

Use WebControl.ToolTip Propertyto set tool tip on Label control.

使用WebControl.ToolTip 属性在 Label 控件上设置工具提示。

Label1.ToolTip = "Your text as tooltip";

回答by Dai

I recommend against using WebControls, I advocate using HtmlControls instead, as they provide a more direct representation of the generated output HTML.

我建议不要使用 WebControls,我提倡使用 HtmlControls,因为它们可以更直接地表示生成的输出 HTML。

Note that asp:Literalis not a WebControl, despite being in the WebControlsnamespace, so it lacks many of the forms-based properties like ToolTip.

请注意asp:Literal,尽管位于WebControls命名空间中,但它不是 WebControl,因此它缺少许多基于表单的属性,例如ToolTip.

The ToolTipproperty is actually a front for the HTML title=""attribute, which can be applied to any visible element. If you're using Literalthen just ensure that you have the title="tip text goes here"attribute in your markup.

ToolTip属性实际上是 HTMLtitle=""属性的前端,可以应用于任何可见元素。如果您正在使用,Literal那么只需确保title="tip text goes here"您的标记中有该属性。

If you're using HtmlControls then you can use the Attributescollection, like so:

如果您使用的是 HtmlControls,那么您可以使用该Attributes集合,如下所示:

HtmlControl div = new HtmlGenericControl("div");
div.Attributes["title"] = "tooltip text here";