明天的明天 永远的永远 未知的一切 我与你一起承担 ??

是非成败转头空 青山依旧在 几度夕阳红 。。。
随笔 - 1277, 文章 - 0, 评论 - 214, 阅读 - 321万
  博客园  :: 首页  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

DataGrid实现tooltip功能

Posted on   且行且思  阅读(437)  评论(0编辑  收藏  举报

1.html代码
<HTML>
    
<HEAD>
        
<title>DataGridTooltip</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<meta name="CODE_LANGUAGE" Content="C#">
        
<meta name="vs_defaultClientScript" content="JavaScript">
        
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
        
<style type="text/css"> .transparent { BORDER-RIGHT: indianred 1px solid; BORDER-TOP: indianred 1px solid; DISPLAY: none; FILTER: alpha(opacity=85); BORDER-LEFT: indianred 1px solid; BORDER-BOTTOM: indianred 1px solid; POSITION: absolute; BACKGROUND-COLOR: infobackground } </style>
        
<script language="javascript">
            
function Show(Country, City, Address, PostalCode, Phone, Fax)
            
{
                
//debugger;
                document.getElementById("td1").innerText="国家:"+Country;
                document.getElementById(
"td2").innerText="城市:"+City;
                document.getElementById(
"td3").innerText="地址:"+Address;
                document.getElementById(
"td4").innerText="邮编:"+PostalCode;
                document.getElementById(
"td5").innerText="电话:"+Phone;
                document.getElementById(
"td6").innerText="传真:"+Fax;
                x 
= event.clientX + document.body.scrollLeft;
                y 
= event.clientY + document.body.scrollTop + 20;
                Popup.style.display
="block";
                Popup.style.left 
= x;
                Popup.style.top 
= y;                
            }

            
function Hide()
            
{
                Popup.style.display
="none";
            }

        
</script>
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="Form1" method="post" runat="server">
            
<div id="Popup" class="transparent">
                
<table border="0" cellpadding="0" bgColor="#00ccff">
                    
<tr>
                        
<td align="center" bgcolor="indianred"><font color="white">联系方式</font></td>
                    
</tr>
                    
<tr>
                        
<td id="td1"></td>
                    
</tr>
                    
<tr>
                        
<td id="td2"></td>
                    
</tr>
                    
<tr>
                        
<td id="td3"></td>
                    
</tr>
                    
<tr>
                        
<td id="td4"></td>
                    
</tr>
                    
<tr>
                        
<td id="td5"></td>
                    
</tr>
                    
<tr>
                        
<td id="td6"></td>
                    
</tr>
                
</table>
            
</div>
            
<asp:DataGrid id="DataGrid1" runat="server">
                
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
                
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
                
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
                
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
                
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
            
</asp:DataGrid>
        
</form>
    
</body>
</HTML>
2.cs代码
public class DataGridTooltip : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataGrid DataGrid1;
        
private DataTable dt;

        
#region Page_Load
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
if(!IsPostBack)
            
{
                SqlConnection cnn 
= new SqlConnection();
                cnn.ConnectionString 
= "data source=meng;initial catalog=Northwind;password=sa;persist security info=True;user id=sa;packet size=4096";

                
string sqlstr = "select Top 16 CustomerID, CompanyName,ContactTitle,Country, City, Address,PostalCode,Phone,Fax from Customers";

                cnn.Open();
                SqlDataAdapter ad 
= new SqlDataAdapter(sqlstr,cnn);
                dt 
= new DataTable();
                ad.Fill(dt);

                DataGrid1.DataSource 
= dt;
                DataGrid1.DataBind();
            }

        }

        
#endregion


        
#region DataGrid1_ItemDataBound
        
private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgs e)
        
{
            
if(e.Item.ItemType == ListItemType.AlternatingItem|| e.Item.ItemType == ListItemType.Item)
            
{
                e.Item.Attributes.Add(
"onmouseover","this.oldcolor=this.style.backgroundColor;this.style.backgroundColor='#C8F7FF';");

                e.Item.Attributes.Add(
"onmousemove",
                    
"Show('"+dt.Rows[e.Item.ItemIndex]["country"].ToString()+"','"
                    
+dt.Rows[e.Item.ItemIndex]["City"].ToString()+"','"                       
                    
+dt.Rows[e.Item.ItemIndex]["Address"].ToString()+"','"                       
                    
+dt.Rows[e.Item.ItemIndex]["PostalCode"].ToString()+"','"
                    
+dt.Rows[e.Item.ItemIndex]["Phone"].ToString()+"','"
                    
+dt.Rows[e.Item.ItemIndex]["Fax"].ToString()+"');");
                e.Item.Attributes.Add(
"onmouseout",    "this.style.backgroundColor=this.oldcolor;Hide();");
            }

        }

        
#endregion


        
#region Web Form Designer generated code
        
override protected void OnInit(EventArgs e)
        
{
            
//
            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
            
//
            InitializeComponent();
            
base.OnInit(e);
        }

        
        
/// <summary>
        
/// Required method for Designer support - do not modify
        
/// the contents of this method with the code editor.
        
/// </summary>

        private void InitializeComponent()
        
{    
            
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
            
this.Load += new System.EventHandler(this.Page_Load);

        }

        
#endregion

    }
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示