在GridView行中设置单元提示,兼容IE,Firefox, Safari

有时候我们需要在Gridview行中某单元格设置提示,要求光标停留在这个单元格上时提示一直显示,没有时间限制,通过层来实现

参考文章:http://cuckoosnest.javaeye.com/blog/470127 (Javascript检测浏览器类型总结)

 

代码如下:

1) 页面代码:

<head runat="server">

    <title>无标题页</title>

   

    <script language="javascript" type="text/javascript">

    function   showTip(msg,e)  

    {  

      document.getElementById("tooltip").innerHTML = msg;

      document.getElementById("tooltip").style.display   =   "block";

    

      var left, top;

      var cursorHeight = 22;  //光标高度

      var nav = function() {  

                    var ua = navigator.userAgent.toLowerCase(), s, o = {};  

                    if( s=ua.match(/msie ([/d.]+)/) ) {  

                        o.ie = true;  

                        o.info = "ie";  

                    } else if( s=ua.match(/firefox//([/d.]+)/) ) {  

                        o.ff = true;  

                        o.info = "firefox";  

                    } else if( s=ua.match(/chrome//([/d.]+)/) ) {  

                        o.chrome = true;  

                        o.info = "chrome";  

                    } else if( s=ua.match(/opera.([/d.]+)/) ) {  

                        o.opera = true;  

                        o.info = "opera";  

                    } else if( s=ua.match(/version//([/d.]+).*safari/) ) {  

                        o.safari = true;  

                        o.info = "safari";  

                    }  

                    if( s && s[1] ) {  

                        o.version = parseFloat( s[1] );  

                    } else {  

                        o.version = 0;  

                    }  

                    o.info = (o.info?o.info:"") + "_" + o.version;   // 获取版本

                    return o.info;  

                }(); 

     

      if(!nav.indexOf("ie")>-1)

      {         

          left = e.clientX  + "px";        

          top = (e.clientY + cursorHeight) + "px";                

      }

      else

      {

            left = window.event.x + document.body.scrollLeft  + "px";

            top = (window.event.y + document.body.scrollTop + cursorHeight) + "px";

       } 

        document.getElementById("tooltip").style.left   =  left;  

        document.getElementById("tooltip").style.top   =   top;

    }  

   

    function   hideTip()  

    {  

        document.getElementById("tooltip").style.display   =   "none";  

    }   

   

    </script>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:GridView ID="gvList" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvList_RowDataBound">

                <Columns>

                    <asp:BoundField HeaderText="name" />

                    <asp:BoundField HeaderText="description" />

                </Columns>

            </asp:GridView>

        </div>

        <div id="tooltip" style="position: absolute; background-color: #FEFFE0; border: 1px   solid   black; padding: 5px 5px; clip: auto; font-size: 9pt; display: none;z-index:9999999">

        </div>

    </form>

</body>

 

 

 

2) 后台代码:

 

protected void Page_Load(object sender, EventArgs e)

    {

        if (!this.IsPostBack)

        {

            #region  Set gridview data and bind data          

            DataTable dt = new DataTable();

            dt.Columns.Add(new DataColumn("name"));

            dt.Columns.Add(new DataColumn("description"));

            dt.Rows.Add("name1", "name1's description");

            dt.Rows.Add("name2", "name2 description is /" name2's description /" ");

            dt.Rows.Add("name3", "name3 description is /r/n /" name3's description /" ");

 

            this.gvList.Columns.Clear();

            System.Web.UI.WebControls.BoundField field = new BoundField();

            field.DataField = "name";

            field.HeaderText = "Name";

            this.gvList.Columns.Add(field);

 

            field = new BoundField();

            field.DataField = "description";

            field.HeaderText = "Description";

            this.gvList.Columns.Add(field);

 

            this.gvList.DataSource = dt;

            this.gvList.DataBind();

            #endregion

        }

    }

    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            e.Row.Cells[0].Text = string.Format("<a href=/"#/" οnmοuseοut=/"hideTip()/" οnmοuseοver=/"showTip('{0}',event)/" οnmοusemοve=/"showTip('{0}',event)/">{1}</a>",

               e.Row.Cells[1].Text.Replace("'", @"/&#39;").Replace("/"", "&quot;").Replace("/r/n", "&lt;br&gt;"), e.Row.Cells[0].Text);

        }

    }

 

 

3)测试在VS2005,XP,IE8,Firefox3.6, Safari3.1 环境中通过

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值