天天看點

gridview 行選中顔色和事件

在gridview 清單中,需要添加如下效果,如果經過時變色,移開時還原成以前的顔色,行選中時通過加深色,進而知道選中了哪行,特别是清單資料比較多的時候,标志很重要,同時觸發選中行事件,那麼如何 行選中加顔色,同時也出發我們事件了?如果我們通入 e.Row.Attributes.Add("onclick", 綁定兩次,發現隻能觸發其中的一個,其實可以把另個放到一個裡面

下面是具體的例子:

protected void EditGridview_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType== DataControlRowType.DataRow)
        {
            //當滑鼠放上去的時候 先儲存目前行的背景顔色 并給附一顔色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='inactivecaptiontext',this.style.fontWeight='';this.style.cursor='hand';");
            //當滑鼠離開的時候 将背景顔色還原的以前的顔色   
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';"); 
            //當滑鼠單擊時,加深色标志
            e.Row.Attributes.Add("onclick", "setvalue();if(window.oldtr!=null){window.oldtr.runtimeStyle.cssText='';}this.runtimeStyle.cssText='background-color:#e6c5fc';window.oldtr=this"); 

        }


    }
           

js代碼:

<script type="text/javascript" >
        function setvalue() {
            alert('行單擊事件,同時變色');
         }
    </script>