C# 下拉列表项的工具提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15872488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tooltip for Drop down list items
提问by Shanna
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?
谁可以帮我这个事?
采纳答案by Soner G?nül
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
回答by Freelancer
Try following code:
尝试以下代码:
foreach (ListItem item in drpID.Items)
{
item.Attributes.Add("Title", item.Text);
}
回答by Sagar Hirapara
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;
}
}
}
回答by Nick DeVore
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. 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.
属性到 ListItem 标签本身,它工作得很好。当然,我收到了一个抱怨,说它不是一个有效的属性,但是当页面呈现时,它出现了,然后它是一个有效的属性。
回答by Glyn Jones
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.
我只需要在以编程方式填充的 DropDownList 上实现工具提示。我发现 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.
漂亮不是。但它有效。
回答by Jamisco
foreach (ToolStripItem item in yourToolStripName.DropDownItems)
{
item.ToolTipText = "tool strip item message";
}