【问题标题】:asp.net HyperLinkField Has no ToolTip property (Alt text)asp.net HyperLinkField 没有 ToolTip 属性(替代文本)
【发布时间】:2010-10-22 16:03:25
【问题描述】:

我希望 HyperLinkField 中有一个 ToolTip 字段,因为 HyperLink 中有一个。 在绑定到我的数据源之前,我正在通过代码创建一个 HyperLinkField:

HyperLinkField hl = new HyperLinkField(); 
hl.DataNavigateUrlFields = new string[] { "col" };
hl.DataNavigateUrlFormatString = "{0}";
hl.DataTextField = "Foo";

有没有什么方法可以为将呈现为工具提示(或替代文本)的内容设置值? 任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net hyperlink tooltip


    【解决方案1】:

    没错,HyperlinkField 中没有 tooltip/alt 文本属性。为了克服这个缺点,您需要使用模板字段并添加常规的超链接控件。

    <asp:TemplateField HeaderText="Href">
        <ItemTemplate>
           <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#
              Eval("Href") %>' Text='<%# Eval("Href") %>' ToolTip='<%# Eval("Text")         %>'> 
           </asp:HyperLink>
        </ItemTemplate>
    </asp:TemplateField>
    

    但是,在程序化广告中执行此操作需要大量工作。您需要创建自己的类来实现 ITemplate 接口。这是一个tutorial

    【讨论】:

    • 当您从网页复制代码时,您应该提供指向该网页的链接。
    【解决方案2】:

    您的要求可以通过在RowDataBoundRowDataBound 事件中为该特定单元格添加工具提示来实现&lt;asp:HyperlinkField&gt; 本身。将 GridView 绑定到 DataSource 后,您可以在 RowDataBound 事件中执行此操作,如下所示:

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[0].ToolTip = "Your tooltip text";
    }
    

    虽然您已经接受了另一个答案,但我的回答可能对其他一些用户有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 2016-08-18
      • 2021-12-19
      相关资源
      最近更新 更多