Sunday 11 November 2012

Show tooltip in Asp.net Dropdownlist items Mouse over Event in C#

Here I going to explain how to show tooltip in dropdownlist items on mouseover event in asp.nt using C#
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.Configuration;
using System.Data;

public partial class ToolTip_in_DropDown_ListItem : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Binddropdown();
        }
    }
    protected void Binddropdown()
    {

        con.Open();
        SqlCommand cmd = new SqlCommand("select EmpNam from Employee", con);
        cmd.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddl.DataSource = ds;
        ddl.DataTextField = "EmpNam";
        ddl.DataValueField = "EmpNam";
        ddl.DataBind();
    }
    protected void ddl_DataBound(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if(ddl.Text!=null)
        {
            foreach (ListItem listItem in ddl.Items)
              {
                   listItem.Attributes["title"] = listItem.Text;
                  //or you can use this
                  //   item.Attributes.Add("Title", listItem.Text);
             } 
        }

    }
}

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

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <center>
     <asp:DropDownList runat="server" ID="ddl" ondatabound="ddl_DataBound" ></asp:DropDownList>
    </center>
   
    </div>
    </form>
</body>
</html>





No comments:

Post a Comment