简体   繁体   English

如何在asp.net c下拉列表中绑定工具提示#

[英]How to bind tool tip in drop down list in asp.net c#

I want to bind tool tip to drop down list item. 我想将工具提示绑定到下拉列表项。 My code is 我的代码是

protected void ddl_DataBound(object sender, EventArgs e)
{
    if (ViewState["HeadToolTip"] != null)
    {
        DataTable dt = ViewState["HeadToolTip"] as DataTable;
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {

                DataRow drow = dt.Rows.Cast<DataRow>().FirstOrDefault(x => x.Field<string>("HeadCode") == li.Value.ToString());
                if (drow != null)
                {
                    li.Attributes["title"] = drow["ToolTip"].ToString();// li.Text;
                }
            }
        }
    }
}

It is working but after selection of any item tool tip is removed from all item list. 它正在工作,但在选择任何项目工具提示后,将从所有项目列表中删除。

You are setting tooltip on DataBound , it might be possible when you are not binding Data within Load or SelectedIndexChange , when you select an item, 2 things occur, SelectedIndexChange event fires and then Page_Load event fires, you are not binding in either one of them I think. 您正在DataBound上设置工具提示,当您未在Load或SelectedIndexChange绑定数据时,当您选择一个项目时,会发生两件事, SelectedIndexChange事件触发,然后Page_Load事件触发,您在其中任何一个中都没有绑定我认为。 That's why somehow, tooltip not working. 这就是为什么不知何故, tooltip不起作用。 You should place your tooltip code within a function and call that function from Page_Load or SelectedIndexChange , so the tooltip will again be binded with DropDownList 您应该将tooltip代码放在一个函数中并从Page_Load或SelectedIndexChange调用该函数,这样tooltip将再次与DropDownList绑定

Hope it solves your issue. 希望它能解决你的问题。 Regards! 问候!

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

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