Add or bind tooltip for dropdownlist items in asp.net by C#

Add or bind tooltip for dropdownlist items in asp.net :



In Previous Post we describe clearly “how to show tooltip ofdropdownlist item in asp.net” and Example Demo for using tooltip for each itemof dropdownlist item using C#. Now we will explain how to add tooltips for each item of dropdown list in asp.net Programming

(Handle Long text problem in dropdownlist using asp.net)

 When we are working to web development in many times we will use dropdownlists in our asp.net web application, if it have more length of data then we will not be able to see entire data within the dropdown list then we use this technique. So here we take a example to bind tooltip forsql database using C# code.

Example of C# Dropdownlist Related Post:


Adding ToolTip of Dropdownlist control:

Now in this post we take an example that consider how to display data as tooltip in C# of each item of list.

Code of dropdownlist data bound method:
protected void myddlist_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"] = li.Text;
            }
        }
    } 
 bind tooltip for dropdownlist items in asp.net by C#

Tooltip in dropdownlist control each item C# code :


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

<!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>tooltips for each item of dropdown list in asp.net Programming for database</title>
    <style type="text/css">
       
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <br />
        <h2>
            Example of Adding or binding ToolTip with each DropdownList Item in C#</h2>
        <p>
            <strong>Select State here and see full data on mouseover event</strong></p>
        <br />
        <br />
        <asp:DropDownList ID="ddlist" runat="server" OnDataBound="ddlist_DataBound" />
    </div>
    </form>
</body>
</html>

DropDownlist_Itemtooltip page C# code Example:


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_Itemtooltip : 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);
        ddlist.DataTextField = "StateName";
        ddlist.DataValueField = "StateID";
        ddlist.DataSource = datatable;
        ddlist.DataBind();
    }
    protected void ddlist_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            foreach (ListItem li in ddl.Items)
            {
                li.Attributes["title"] = li.Text;
            }
        }
    }
    protected void ddlist_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
} 


Other Asp.net Related Post :

Comments