GridView中单击某行变色(前后台2种方式)

本文介绍了一种实现表格行点击高亮的方法,包括前台JavaScript实现和后台.NET实现,使得用户可以直观地查看所选行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前台方式:  


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

       

    function SetBGC()

    {

        var style;

        var obj = document.getElementById("lv_roleList").getElementsByTagName("tr");

        for(var i=0; i<obj.length; i++){

            obj[i].onclick=function(){

                style = this.style.background; //记录当前样式 (背景)

 

                for(var j=0; j<obj.length; j++){

                    obj[j].style.background = ""; //将所有行的样式清空

                }

 

            this.style.background = style; //还原当前行样式

            this.style.background = this.style.background == "#ff0000" ? "" : "#ff0000"; //调整将当前行样式

            }

        }

    }

    if(window.attachEvent)

        window.attachEvent("onload",SetBGC);

    </script>

 

 

后台方式:

 

 

 

protected override void Render(HtmlTextWriter writer)

    {

        foreach (GridViewRow row in this.lv_roleList.Rows)

        {

            if (row.RowType == DataControlRowType.DataRow)

            {

                row.Attributes["onclick"] = ClientScript.GetPostBackEventReference(this.lv_roleList, "Select$" + row.RowIndex.ToString(), true);

                row.Attributes["style"] = "cursor:pointer";

                row.Attributes["title"] = "单击选择行";

                row.Attributes.Add("onclick", "if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#99cc00';window.oldtr=this");

            }

        }

 

        base.Render(writer);

    }

 

从别处摘来的,好不好用还没试过,暂时留做收藏,以备后用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值