4

I have a dropdownlist and I want to add tooltip for dropdownlist items. I tried with following code, but it does not work;

for (int d = 0; d < drpID.Items.Count; d++)
{
    drpID.Items[d].Attributes.Add("title", drpID.Items[d].Value);          
}

Can anyone help me on this?

6 Answers 6

8

Try like this;

public void Tooltip(ListControl lc)
{
    for (int d = 0; d < lc.Items.Count; d++)
    {
        lc.Items[d].Attributes.Add("title", lc.Items[d].Text);
    }
}

You should use .Text property for tooltip, not for .Value.

Check out for this link: http://www.dotnetspider.com/resources/5099-Tool-tip-for-DropDownList-ASP-NET.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

@SandraDsouza It should work. How did you try this? Do you send your dropdownlist as a parameter?
I had made a mistake while sending the parameter. Thanks again
4

Try following code:

foreach (ListItem item in drpID.Items)
{
item.Attributes.Add("Title", item.Text);
}

Comments

2

You should try this

 protected void ddlDetails_DataBound(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;
    if(ddl!=null)
  {
    foreach (ListItem li in ddl.Items)
    {
      li.Attributes["title"] = li.Text;
    } 
  }
}

enter image description here

Comments

1

I know this thread was regarding a databound control, but in the infrequent case where you have actually hard coded the ListItem values in the aspx page, I simply added a

<asp:ListItem Id="liNumberOne" Runat="server" title="My nifty help text" />

attribute to the ListItem tag itself and it worked just fine. Sure, I got a complaint about that not being a valid attribute but when the page rendered, it came along and then it WAS a valid attribute.

Comments

1

I've just had to implement a tooltip on a programatically populated DropDownList. I found that the title attribute was being set to the Text property automatically and I couldn't override it, even at PreRender.

None of the suggestions on this thread worked, and I was eventually forced to use a jQuery approach.

When creating the list item I set a custom attribute with the tooltip text

    ListItem item = new ListItem("Name", "Value");
    item.Attributes.Add("tooltip", tooltip);
    ddl.Items.Add(item);

Then fire the method below with a start up script

function SetTooltip() {
    jQuery('#<%=ddl.ClientID %> option').each(
        function () {
            jQuery(this).attr('title', jQuery(this).attr('tooltip'));
        }
    );
}

Pretty it ain't. But it works.

Comments

0
        foreach (ToolStripItem item in yourToolStripName.DropDownItems)
        {
            item.ToolTipText = "tool strip item message";
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.