简体   繁体   English

下拉列表项的工具提示

[英]Tooltip for Drop down list items

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? 谁可以帮我这个事?

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 . 您应该使用工具提示的.Text属性,而不是.Value

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

Try following code: 请尝试以下代码:

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

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

在此输入图像描述

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 我知道这个线程是关于数据绑定控件的,但在不经常的情况下,你实际上在aspx页面中硬编码了ListItem值,我只是添加了一个

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

attribute to the ListItem tag itself and it worked just fine. 属性为ListItem标签本身,它工作得很好。 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. 当然,我得到的投诉是,这不是一个有效的属性,但当页面呈现时,它出现了,然后它是一个有效的属性。

I've just had to implement a tooltip on a programatically populated DropDownList. 我只需要在以编程方式填充的DropDownList上实现工具提示。 I found that the title attribute was being set to the Text property automatically and I couldn't override it, even at PreRender. 我发现title属性被自动设置为Text属性,我无法覆盖它,即使在PreRender也是如此。

None of the suggestions on this thread worked, and I was eventually forced to use a jQuery approach. 这个线程上没有任何建议有效,我最终被迫使用jQuery方法。

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. 但它的确有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM