在使用C#寫CRM時遇到一個問題,GridView綁定了了資料後,我想在滑鼠停留到GridView資料行時,行變色反白該行,並且滑鼠點擊該行時,將該條目的資料顯示在GridView下面的控制項自動將該行對應的資料顯示出來。
我的實現步驟是:
1.增加GridView的GVSelect_RowDataBound事件
protected void GVSelect_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//當滑鼠停留時更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#8EC26F'");
//當滑鼠移開時還原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//設定懸浮滑鼠指標形狀為"小手"
e.Row.Attributes["style"] = "Cursor:hand";
//單擊/雙擊 事件
e.Row.Attributes.Add("OnClick", "ClickEvent('" + e.Row.Cells[0].Text + "')");
//註:OnClick參數是指明為按一下滑鼠時間,後個是調用javascript的ClickEvent函數
}
}
2.在頁面的HTML裡添加javascript函數,用來響應滑鼠點擊事件,因為ASP用戶端不能主動調用服務端的函數,我在這裡添加一個LinkButton,將其text設定為空白,然後在ClickEvent(cId)函數中,調用這個LinkButton的單擊事件。
<script language="javascript">
function ClickEvent(cId)
{
//調用LinkButton的單擊事件,btnBindData是LinkButton的ID
document.getElementById("btnBindData").click();
}
</script>
3.添加LinkButton的響應事件
protected void btnBindData_Click(object sender, EventArgs e)
{
//在這裡對你需要的資料資訊進行輸出
SetClientInfo();//我的處理函數
}
GridView滑鼠停留變色和單擊處理事件,當滑鼠在GridView的行上停留時,將該行變色,當單擊該行時,做相應處理,記得要在<gridview ></gridview> 之間 加上RowDataBound=GVSelect_RowDataBound 屬性