Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net HyperLinkField Has no ToolTip property (Alt text)

I wish there was a ToolTip field in HyperLinkField as there is one in HyperLink. I'm creating a HyperLinkField by code before binding to my data source:

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

Is there any way to also set a value to something which will render as a tooltip (or alt text)? Any help will be appreciated.

like image 272
user97889 Avatar asked Apr 29 '09 18:04

user97889


2 Answers

Your requirement can be accompished in <asp:HyperlinkField> itself by adding tooltip for that specific cell in RowDataBound event of a GridView. After binding the GridView to your DataSource you can do this in a RowDataBound event as follows:

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

Though you have accepted another answer, my answer may be helpful for some other users!

like image 189
banupriya Avatar answered Oct 22 '22 10:10

banupriya


That's correct, there's no tooltip/alt text property in a HyperlinkField. To get around this shortcoming, you need to use a template field and add a regular Hyperlink control.

<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>

However, doing this in a programmatic requires a lot of work. You need to create your own class that implements the ITemplate interface. Here's a tutorial on that.

like image 30
Jose Basilio Avatar answered Oct 22 '22 09:10

Jose Basilio