Click here to Skip to main content
15,900,461 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear All,

How to display tooltip from database for dropdownlist items in asp.net c#
-------------------------------------------------------------------------

On my webpage i have Dropdownlist with CollegeCodes in Dropdownlist, These values are displaying from database.

In SqlDatabase table(Colleges) I have two columns as CollegeCode, CollegeFullName.

Now I want to display FullName of College with ToolTip according to the college code.

For Example:

the college Code in dropdownlist is CBIT , when the user mouseover on it, it must show ToolTip as Chaitanya Bharati Institute of Technology AS TOOLTIP.

Please help me,

Thanks in advance.
Posted

try it working perfectly


aspx page:


XML
<asp:DropDownList ID="DropDownList1" runat="server"
            ondatabound="DropDownList1_DataBound">
        </asp:DropDownList>



aspx.cs

SqlConnection con = new SqlConnection("your connection string");
protected void Page_Load(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select code,collegename from table", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "code";
DropDownList1.DataValueField = "collegename";
DropDownList1.DataBind();
}
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
if (ddl != null)
{
foreach (ListItem li in ddl.Items)
{
li.Attributes["title"] = li.Value;
}
}
}
 
Share this answer
 
Comments
Member239258 6-Nov-13 5:59am    
Thanks, its working fine,
But also i need the tool tip text to be all capital and the tool tip must be yellow background.

Please help thanks.
Raajkumar.b 6-Nov-13 6:05am    
ok i will try and let u know. If it is correct answer mark it as answer
Member 7909353 31-Jan-17 1:25am    
It is not working after selection
You just neet to set title of your drop down along with the Text and Value. Follow the below code I am sure you will get your answer

XML
<asp:DropDownList ID="ddlAboutUs" runat="server" >
       <asp:ListItem title="One" Text="1" Value="1"></asp:ListItem>
       <asp:ListItem title="Two" Text="2" Value="2"></asp:ListItem>
       <asp:ListItem title="Three" Text="3" Value="3"></asp:ListItem>
   </asp:DropDownList>
 
Share this answer
 
Comments
Member239258 6-Nov-13 4:59am    
hello sir,
am taking the values from table.
so, please help me how to do.
thanks.
Raj Parashar 6-Nov-13 5:05am    
plz try this

ddlAboutUs.DataSource = somedatasource();
ddlAboutUs.DataTextField = "Name";
ddlAboutUs.DataValueField = "Value";
ddlAboutUs.ToolTip = "Name";
Karthik_Mahalingam 6-Nov-13 5:55am    
Hi.ToolTip="Name" , it will show the tooltip as "name" to the control and to all the items in the dropdownlist..
Raj Parashar 6-Nov-13 6:38am    
Name in the sense Tooltip text database field name.
Karthik_Mahalingam 6-Nov-13 7:34am    
try it.. its tooltip for the control not the items
Simple way, try it.


C#
protected void BindDropDownList(DropDownList ddl, DataTable dataSource)
       {
           foreach (DataRow row in dataSource.Rows)
           {
               var item = new ListItem();
               item.Text = row["name"].ToString();
               item.Attributes["title"] = row["college"].ToString();
               ddl.Items.Add(item);
           }
       }
 
Share this answer
 
Comments
Member239258 6-Nov-13 6:08am    
I need tool tip as uppercase and background of tooltip must be yellow.

Please help

thanks.
Raajkumar.b 6-Nov-13 6:50am    
check the solution 4 for tool tip text to upper case but to change the tooltip background color we have to write java script search it in google you will get so many links....
To change text to upper case just write
li.Value.ToUpper(); in the place of li.value;
 
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