如何在gridview中使用光棒呢
- 2007-11-15
 - 21205
 - 0
 
光棒(LightBar)的效果就是滑鼠移到每一個row的時候都會顯示另一種顏色,讓使用者清楚的知道目前在看哪行,市面上寫法百百款,不過demo比較喜歡使用此文的寫法,所以提供出來給各位看一下。
您可以參考 demo 另一篇使用 jQuery 所寫的光棒效果 萬用版表格光棒效果 LinghtBar 使用 jQuery
◆首先開啟您的web.config來加上設定值。
- 
	
<appSettings>
 - <add key="changeColor" value="#F7F7F6"/>
 - <add key="backgroundColor" value="#FBFBFB"/>
 - </appSettings>
 
◆ 要使GridView擁有光棒的效果必須把事件寫在RowCreated事件或是RowDataBound事件裡面 ▲如果您的GridView是單色的就可以這樣寫
- 
	
if (e.Row.RowType == DataControlRowType.DataRow)
 - //判定row的型態是資料行
 - {
 - e.Row.Attributes.Add("onmouseover", string.Format("this.style.backgroundColor='{0}';", changeColor));
 - //滑鼠移到變色
 - e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF';");
 - //滑鼠移開底色恢復為白色
 - }
 
▲如果您的GridView是雙色的就可以這樣寫
- 
	
if (e.Row.RowType == DataControlRowType.DataRow)
 - //判定row的型態是資料行
 - {
 - e.Row.Attributes.Add("onmouseover", string.Format("this.style.backgroundColor='{0}';",ConfigurationManager.AppSettings["changeColor"].ToString()));
 - //滑鼠移到變色
 - if (e.Row.RowState == DataControlRowState.Alternate)
 - //判定row的型態是替代行
 - e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='White';");
 - //滑鼠移開底色恢復為白色
 - else
 - e.Row.Attributes.Add("onmouseout", string.Format("this.style.backgroundColor='{0}';", ConfigurationManager.AppSettings["backgroundColor"].ToString()));
 - //滑鼠移開底色恢復為設定好的底色
 - }
 
?小鋪廢言:這樣子寫的好處就在於如果GridView的色系改變了,只需要改變web.config的色碼就好了,而且不需要每次呼叫一次就打一次色碼








Roslyn 魔法工坊:打造你的 Source Generator [2025-12-20]開課 共7H
回應討論