Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have bound the values for dropdown list dynamically and i ve the strange case that the title attribute is working for a set of controls and not for other set of controls.

In the following case tooltip is showing,
C#
foreach (var description in descriptions)
                    {
                        var item = new ListItem();

                        item.Text = Convert.ToString(description.Title);
                        item.Value = Convert.ToString(description.ID);
                        item.Attributes["title"] = Convert.ToString(description.Description);
                        ddlDescription.Items.Add(item);
                    }


And in the following case its not working,

C#
foreach (var causeDetail in causeDetails)
                           {
                               var item = new ListItem();

                               item.Text = causeDetail.Title;
                               item.Value = Convert.ToString(causeDetail.ID);
                               item.Attributes["title"] = causeDetail.ToolTip;

                               ddlSection.Items.Add(item);
                           }


I have debugged the codes and I am sure that causeDetail.ToolTip has some values in it. Please help me in this case,
Thanks in advance.
Posted

Try doing the same in DataBound Event of DropDownList.
C#
protected void ddlSection_DataBound(object sender, EventArgs e)
{
    DropDownList ddl = sender as DropDownList;
    if(ddl!=null)
    {
        foreach (ListItem li in ddl.Items)
        {
            li.Attributes["title"] = li.Text;
        }
    }
}


--Amy
 
Share this answer
 
Comments
Strikerzz Arut 5-Aug-14 11:14am    
The above solution could not reflect the ddl.Items right?
Do you want me to bind the values again?
_Amy 5-Aug-14 11:31am    
You should bind your DropDown like this:
ddl.DataSource = causeDetails;
ddl.DataTextField = "Title";
ddl.DataValueField = "ID"
ddl.DataBind();
ddl.DataBound += ddlSection_DataBound;
Strikerzz Arut 5-Aug-14 11:34am    
This is okay but after updating the list items i.e..,
li.Attributes["title"] = li.Text; again v need to bind it?
_Amy 5-Aug-14 11:38am    
No. You don't need to rebind it. You don't need to loop and add the items to it. While binding the dropdown using above code snippet, databound event will be fired. And there you can set your title.
Strikerzz Arut 5-Aug-14 11:52am    
The above code is to update the listitem li alone and I have to know how to use that li to update the ddl.items? The ddl.attributes["title"] can be used but it cannot reflect the values in all individual items. The final value in the foreach will be added to all the values as tooltip.

If u consider the above codes, both are in same format and 1 is working and the another is not working. Please correct me.
item.Attributes["title"] = causeDetail.ToolTip.ToString(); does it. Thanks _Amy for your response.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900