如何在gridview中使用光棒呢
- 4962
- 1
光棒(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的色碼就好了,而且不需要每次呼叫一次就打一次色碼
網友回應
- Longer

- 發表於: 2008-07-14
- 這樣的寫法,如果原來的Row有設定顏色,當你移上又移下後,就會都變成白色吧?
-
你如果使用第二種寫法的話,就可以預先設定好,你ROWS的顏色讓它作切換,不然我再發一篇會自己去抓底色的好了
回覆於: 2008-07-19