Add tooltip for dropdownlist items in asp.net with C# Example

Tooltip for dropdownlist items



In this Post explain how to add tooltip for dropdownlistitem in asp.net Programming. Some time the developers need this type of solution in web form when he wants to use dropdownlist with large text field. Then this is also called “Handle Long text problem in dropdownlist “.

How to display or add tooltip for dropdownlist items in asp.net:

Now we give the code here.


Code for the Add tooltip in dropdownlist:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownlist_Item_tooltip.aspx.cs" Inherits="DropDownlist_Item_tooltip" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Bind Tooltips for Dropdownlist</title>
    <style type="text/css">

 p.MsoNormal
       {margin-top:0in;
       margin-right:0in;
       margin-bottom:10.0pt;
       margin-left:0in;
       line-height:115%;
       font-size:11.0pt;
       font-family:"Calibri","sans-serif";
       }
    </style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
    <br />
    <p class="MsoNormal">
&nbsp;tooltip Example in dropdownlist items for asp.net</p>
    Select State here
    <br />
    <br />
<asp:DropDownList ID="ddlState" runat="server" ondatabound="ddlState_DataBound"/>
</div>
</form>
</body>
</html>

 
tooltip in dropdownlist in asp.net
tooltip in dropdownlist in asp.net

C# Code for the Add tooltip in dropdownlist:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class DropDownlist_Item_tooltip : System.Web.UI.Page
{
    string _connectionstring;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            _connectionstring = @"Data Source=PARIJAT-PC\PARIJAT;Initial Catalog=Database1;Integrated Security=True;Pooling=False";
            BindData();
        }
    }

    protected void BindData()
    {


        string _sql = "select StateID,StateName from States";
        SqlConnection _connection = new SqlConnection(_connectionstring);
        SqlCommand _command = new SqlCommand(_sql, _connection);
        SqlDataAdapter _adapter = new SqlDataAdapter(_command);
        DataTable datatable = new DataTable();
        _adapter.Fill(datatable);
        ddlState.DataTextField = "StateName";
        ddlState.DataValueField = "StateID";
        ddlState.DataSource = datatable;
        ddlState.DataBind();
    }
    protected void ddlState_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"] = li.Text;
            }
        }
    }
}

Comments

Popular posts from this blog