實現(xiàn)原理:
為 DataGrid 中的每一行,綁定onmouseover、onmousemove、onmouseout事件,使的鼠標移動到行內時,自動顯示一個<div>,鼠標移出該行,就把這個<div>隱藏掉。
實現(xiàn)代碼:
前臺:
1. 定義<div>的樣式:
<style type="text/css">
.transparent { FILTER: alpha(opacity=85);
BORDER-TOP: indianred 1px solid;
BORDER-RIGHT: indianred 1px solid;
BORDER-LEFT: indianred 1px solid;
BORDER-BOTTOM: indianred 1px solid;
POSITION: absolute;
BACKGROUND-COLOR: infobackground;
DISPLAY: none }
</style>
2. 顯示和隱藏窗體的腳本:
<script language=" JavaScript ">
// 顯示彈出窗體
function Show(Country, City, Address, PostalCode, Phone, Fax)
{
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軸的坐標
x = event.clientX + document.body.scrollLeft;
// 獲得鼠標的Y軸的坐標
y = event.clientY + document.body.scrollTop + 20;
// 顯示彈出窗體
Popup.style.display="block";
// 設置窗體的X,Y軸的坐標
Popup.style.left = x;
Popup.style.top = y;
}
// 隱藏彈出窗體
function Hide()
{
// 隱藏窗體
Popup.style.display="none";
}
</script>
3. ToolTip 窗體的代碼
<div id="Popup" class="transparent" style=" Z-INDEX: 200">
<table border="0" cellpadding="0" style="FONT-SIZE: x-small">
<tr>
<td align="middle" bgcolor="indianred"><font color="white"> 聯(lián)系方式 </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>
后臺:
private DataTable dt;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if(!IsPostBack)
{
SqlConnection cnn = new SqlConnection();
cnn.ConnectionString = "data source=localhost;initial catalog=Northwind;password=;"
+"persist security info=True;user id=sa;workstation id=APJ062;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);
grdCustomer.DataSource = dt;
grdCustomer.DataBind();
}
}
private void grdCustomer_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();");
}
}
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
