繁体   English   中英

下拉列表项的工具提示

[英]Tooltip for Drop down list items

我有一个下拉列表,我想为下拉列表项添加工具提示。 我尝试使用以下代码,但它不起作用;

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

    }

谁可以帮我这个事?

试试这样;

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

您应该使用工具提示的.Text属性,而不是.Value

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

请尝试以下代码:

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

你应该试试这个

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

在此输入图像描述

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

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

属性为ListItem标签本身,它工作得很好。 当然,我得到的投诉是,这不是一个有效的属性,但当页面呈现时,它出现了,然后它是一个有效的属性。

我只需要在以编程方式填充的DropDownList上实现工具提示。 我发现title属性被自动设置为Text属性,我无法覆盖它,即使在PreRender也是如此。

这个线程上没有任何建议有效,我最终被迫使用jQuery方法。

创建列表项时,我使用工具提示文本设置自定义属性

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

然后使用启动脚本触发下面的方法

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

漂亮不是。 但它的确有效。

        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