實現GridView Tooltip的功能

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>無標題頁</title>
  6.     <script language="javascript" type="text/javascript">
  7.         function selectkk()
  8.         {
  9.          var item = document.getElementById('Select1');
  10.          // window.alert("sddsdsd");
  11.           if(item.selectedIndex!=0)
  12.           {
  13.             window.alert(item.options[item.selectedIndex].text);
  14.           }
  15.           else
  16.           {
  17.              window.alert("is 0");
  18.           }
  19.           
  20.         }
  21.         
  22.         function showDetail(cell1,cell2)
  23.         {
  24.            document.getElementById('td1').innerText="Name:"+cell1;
  25.            document.getElementById('td2').innerText="Address:"+cell2;
  26.            //獲得鼠標的X軸的座標
  27.             x = event.clientX + document.body.scrollLeft;
  28.              
  29.            //獲得鼠標的Y軸的座標
  30.           y = event.clientY + document.body.scrollTop+10;
  31.            
  32.             //顯示彈出窗體
  33.            Popup.style.display="block";
  34.           
  35.            //設置窗體的X,Y軸的座標
  36.            Popup.style.left = x;
  37.            Popup.style.top = y;
  38.         }
  39.          //隱藏彈出窗體
  40.         function hide()
  41.       {
  42.              //隱藏窗體
  43.            Popup.style.display="none";
  44.          }
  45.     </script>
  46.        <style type="text/css">
  47.      .transparent 
  48.      { BORDER-RIGHT: indianred 1px solid; 
  49.      BORDER-TOP: indianred 1px solid;
  50.       DISPLAY: none; FILTER: alpha(opacity=85);
  51.        BORDER-LEFT: indianred 1px solid; 
  52.        BORDER-BOTTOM: indianred 1px solid; 
  53.        POSITION: absolute; 
  54.        BACKGROUND-COLOR: infobackground; 
  55.        font-color;red;}
  56.        </style>
  57. </head>
  58. <body>
  59.     <form id="form1" runat="server">
  60.     <div>
  61.         <table border="1" style="width: 50%">
  62.             <caption>
  63.             </caption>
  64.             <tr>
  65.                 <td style="width: 100px">
  66.                 </td>
  67.                 <td style="width: 100px">
  68.                     <select id="Select1" style="width: 109px" name="slt">
  69.                         <option value="0"><--seleted--></option>
  70.                         <option value="1">zhanpeng</option>
  71.                         <option value="2">zhanjia</option>
  72.                         <option value="3">lee jing</option>
  73.                         
  74.                     </select>
  75.                 </td>
  76.                 <td style="width: 122px">
  77.                     <input id="Button1" type="button" value="button" onclick="selectkk();" runat="server" /></td>
  78.             </tr>
  79.             <tr>
  80.                 <td style="width: 100px">
  81.                 </td>
  82.                 <td style="width: 100px">
  83.                 </td>
  84.                 <td style="width: 122px">
  85.                 </td>
  86.             </tr>
  87.             <tr>
  88.                
  89.                 <td style="width: 100px; height: 30px;">
  90.                 </td>
  91.                
  92.                 <td style="width: 100px; height: 30px;">
  93.                      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound" Width="326px" OnRowCreated="GridView1_RowCreated">
  94.                     <Columns>
  95.                     
  96.                      <asp:BoundField DataField="Name" HeaderText="Name"/>
  97.                      <asp:BoundField DataField="Address" HeaderText="Address" />
  98.                     </Columns>
  99.                     </asp:GridView>
  100.                 </td>
  101.                 <td style="width:300; height: 30px;">
  102.                   <div id="Popup" class="transparent" style="Z-INDEX:200;">
  103.                     <table style="width: 300">
  104.                         <tr>
  105.                             <td style="width: 100px" id="td1">
  106.                             </td>
  107.                            
  108.                         </tr>
  109.                         <tr>
  110.                             <td style="width: 100px; height: 21px;" id="td2">
  111.                             </td>
  112.                         </tr>
  113.                     </table>
  114.                     </div>
  115.                 </td>
  116.             </tr>
  117.         </table>
  118.     
  119.     </div>
  120.     </form>
  121. </body>
  122. </html>
  123. ----------------------------
  124. using System;
  125. using System.Data;
  126. using System.Configuration;
  127. using System.Web;
  128. using System.Web.Security;
  129. using System.Web.UI;
  130. using System.Web.UI.WebControls;
  131. using System.Web.UI.WebControls.WebParts;
  132. using System.Web.UI.HtmlControls;
  133. using System.Data.SqlClient;
  134. public partial class _Default : System.Web.UI.Page 
  135. {
  136.     protected void Page_Load(object sender, EventArgs e)
  137.     {
  138.         if (!this.IsPostBack)
  139.         {
  140.             getDate();
  141.         }
  142.     }
  143.     public void getDate()
  144.     {
  145.         SqlConnection con = new SqlConnection("server=server01;database=test;uid=sa;pwd=sql");
  146.         con.Open();
  147.         SqlDataAdapter da = new SqlDataAdapter("select * from tab", con);
  148.         DataSet ds = new DataSet();
  149.         da.Fill(ds);
  150.         GridView1.DataSource = ds;
  151.         GridView1.DataBind();
  152.     }
  153.     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  154.     {
  155.         if (e.Row.RowType == DataControlRowType.DataRow)
  156.         {
  157.             e.Row.Attributes.Add("onmouseover""currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';showDetail('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[1].Text + "')");
  158.         
  159.             e.Row.Attributes.Add("onmouseout""hide();this.style.backgroundColor=currentcolor;");
  160.            
  161.         }
  162.     }
  163.     protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
  164.     {
  165.         //if (e.Row.RowType == DataControlRowType.DataRow)
  166.         //{
  167.         //    e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#C0C0FF';this.style.cursor='hand';");
  168.         //    ////當鼠標移走時還原該行的背景色
  169.         //    //e.Row.Attributes.Add("onmouseover", "showDetail('" + e.Row.Cells[0].Text + "','" + e.Row.Cells[1].Text + "')");
  170.         //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;hide();");
  171.         //}
  172.     }
  173. }

這段代碼是網上找的,但是我發現當拖動右邊的滾動條以後,這個TIP就跑到屏幕的可見處以外的地方去了,我在這裏對其進行修正,希望對大家有幫助:

 

 

更改此函數:

  1. function showDetail(cell1,cell2)
  2.         {
  3.            document.getElementById('td1').innerText="Name:"+cell1;
  4.            document.getElementById('td2').innerText="Address:"+cell2;
  5.            //獲得鼠標的X軸的座標
  6.             x = event.clientX + document.body.scrollLeft;
  7.              
  8.            //獲得鼠標的Y軸的座標
  9.           y = event.clientY + document.body.scrollTop+10;
  10.            
  11.             //顯示彈出窗體
  12.            Popup.style.display="block";
  13.           
  14.            //設置窗體的X,Y軸的座標
  15.            Popup.style.left = x;
  16.            Popup.style.top = y;
  17.         }

 

  1. function showDetail(cell1,cell2)
  2.         {
  3.            document.getElementById('td1').innerText="Name:"+cell1;
  4.            document.getElementById('td2').innerText="Address:"+cell2;
  5.            //獲得鼠標的X軸的座標 
  6.             panelAdmin=document.getElementById("Popup");
  7.         var bt=event.srcElement||event.target;
  8.         panelAdmin.style.display="block";
  9.         panelAdmin.style.left=bt.offsetLeft+20+"px";
  10.         panelAdmin.style.top=realOffset(bt).y+bt.offsetHeight+5+"px";
  11.     }

 

然後添加此javascript函數

 

  1. function realOffset(o)
  2.         {
  3.           var x = y = 0; do{
  4.           x += o.offsetLeft || 0; 
  5.           y += o.offsetTop  || 0;
  6.           o  = o.offsetParent;}while(o);
  7.           return {"x" : x, "y" : y};
  8.         }

 

如果大家有什麼不明白的地方請與我聯繫

 

----power by 飛鳥工作室

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章