Tooltip for Drop down list items

2019-02-11 00:52发布

问题:

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?

回答1:

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



回答2:

Try following code:

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


回答3:

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



回答4:

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.



回答5:

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.



回答6:

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