Step 1 :- Design Page
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Show
tooltip.aspx.cs" Inherits="Demo.Show_tooltip" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Show
tooltip in Asp.net Dropdownlist items inside the GridView on Mouse over Event
in C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>DropDownList
Inside the GridView</b>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDataBound="GridView1_RowDataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Sr
No">
<ItemTemplate>
<asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex
+ 1 %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Drop
Down List">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
</div>
</form>
</body>
</html>
Step 2
:- Code behind Page
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class Show_tooltip :
System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindGridView();
}
}
protected DataTable GenerateData()
{
DataTable dt
= new DataTable();
DataRow dr
= null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
int totalRow
= 50;
for (int i
= 0; i < totalRow; i++)
{
dr = dt.NewRow();
dr["RowNumber"] = i + 1;
dr["Column1"] = "What is My Row Number ? Ans. - " +
(i + 1);
dt.Rows.Add(dr);
}
return dt;
}
protected void BindGridView()
{
GridView1.DataSource = GenerateData();
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType
== DataControlRowType.DataRow)
{
DropDownList dropDownList
= (DropDownList)e.Row.FindControl("DropDownList2");
dropDownList.DataSource = GenerateData();
dropDownList.DataTextField = "Column1";
dropDownList.DataValueField = "RowNumber";
dropDownList.DataBind();
dropDownList.Items.Insert(0, "-Select-");
#region Using foreach Loop
foreach (ListItem listitem in dropDownList.Items)
{
listitem.Attributes["title"] = listitem.Text;
}
#endregion
}
}
}
}
Note :-
Alternatively You can use the For Loop also to
show tooltip in Asp.net DropDownList Control.
Example...
#region Using
for Loop
for (int i
= 0; i < DropDownList1.Items.Count; i++)
{
DropDownList1.Items[i].Attributes.Add("title",
DropDownList1.Items[i].Text);
}
#endregion
No comments:
Post a Comment