简体   繁体   English

以编程方式将工具提示放在ListBox类上

[英]Putting tooltips programmatically on ListBox class

I have created an asp:listbox and in the cs file, I am trying to bind it to a datasource. 我创建了一个asp:listbox,在cs文件中,我试图将它绑定到数据源。 However, I want to put in tooltips for each option so that when you hover over a multiple-listbox you will see the name of the item in both the listbox and the tooltip. 但是,我想为每个选项添加工具提示,这样当您将鼠标悬停在多列表框上时,您将在列表框和工具提示中看到该项目的名称。

The following code is what I have tried (which obviously will not work), which will just put a big tooltip for the "select" html tag, instead of the "option" tags individually. 下面的代码是我尝试过的(显然不起作用),它只是为“select”html标记添加了一个很大的工具提示,而不是单独的“option”标记。

I think I need a loop, but I'm not sure how to add attributes to each element and the ASP documentation is no help. 我想我需要一个循环,但我不确定如何为每个元素添加属性,ASP文档没有帮助。 I bet it's an easy solution, but I can't figure out how to do it. 我敢打赌这是一个简单的解决方案,但我无法弄清楚如何做到这一点。

  LBRangeOfUsers.DataSource = GetSource();
  LBRangeOfUsers.DataValueField = "id";
  LBRangeOfUsers.DataTextField = "desc";
  LBRangeOfUsers.Attributes.Add("title", "desc");
  LBRangeOfUsers.DataBind();

I tried this: 我试过这个:

  int i = 0;
  foreach (ListItem li in LBRangeOfUsers.Items)
  {
      LBRangeOfUsers.Items[i].Attributes.Add("title", li.Value);
      i++;
  }

It worked for me like this: 它对我有用:

LBRangeOfUsers.DataSource = GetSource();
LBRangeOfUsers.DataBind();

foreach (ListItem item in LBRangeOfUsers.Items)
    item.Attributes["title"] = item.Value;

make sure the loop runs after the binding. 确保绑定后循环运行。

您是否尝试过循环LBRangeOfUsers.Items并为每个ListItem添加属性?

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

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