protected void gvwDepartment_RowDataBound(object sender, GridViewRowEventArgs e)
{//判斷是否是資料行
if (e.Row.RowState == DataControlRowState.Edit)
{ // 編輯狀態
e.Row.Attributes.Remove("onclick");
e.Row.Attributes.Remove("ondblclick");
e.Row.Attributes.Remove("style");
e.Row.Attributes["title"] = "編輯行";
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//滑鼠移動到某行上,該行變色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccddee'");
//滑鼠移開後,恢復
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
//滑鼠顯示為小手狀態
e.Row.Attributes["style"] = "Cursor:hand";
}
}
protected override void Render(HtmlTextWriter writer)
{
// GridView
foreach (GridViewRow row in gvwDepartment.Rows)
{
if (row.RowState == DataControlRowState.Edit)
{ // 編輯狀態
row.Attributes.Remove("onclick");
row.Attributes.Remove("ondblclick");
row.Attributes.Remove("style");
row.Attributes["title"] = "編輯行";
continue;
}
if (row.RowType == DataControlRowType.DataRow)
{
// 單擊事件,為了響應雙擊事件,需要延遲單擊響應,根據需要可能需要增加延遲
// 獲取ASP.NET內建回髮指令碼函式,返回 __doPostBack(<<EventTarget>>, <<EventArgument>>)
// 可直接硬編碼寫入指令碼,不推薦
row.Attributes["onclick"] = String.Format("javascript:setTimeout(\"if(dbl_click){{dbl_click=false;}}else{{{0}}};\", 1000*0.3);", ClientScript.GetPostBackEventReference(gvwDepartment, "Select$" + row.RowIndex.ToString(), true));
// 雙擊,設定 dbl_click=true,以取消單擊響應
row.Attributes["ondblclick"] = String.Format("javascript:dbl_click=true;window.location.href='UpdateDepartment.aspx?DepartmentID={0}';", gvwDepartment.DataKeys[row.RowIndex].Value.ToString());
//
row.Attributes["style"] = "cursor:pointer";
row.Attributes["title"] = "單擊選擇行,雙擊開啟詳細頁面";
}
}
base.Render(writer);
}
protected void gvwDepartment_RowDataBound(object sender, GridViewRowEventArgs e)
{//判斷是否是資料行
if (e.Row.RowState == DataControlRowState.Edit)
{ // 編輯狀態
e.Row.Attributes.Remove("onclick");
e.Row.Attributes.Remove("ondblclick");
e.Row.Attributes.Remove("style");
e.Row.Attributes["title"] = "編輯行";
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
//滑鼠移動到某行上,該行變色
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ccddee'");
//滑鼠移開後,恢復
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
//滑鼠顯示為小手狀態
e.Row.Attributes["style"] = "Cursor:hand";
}
}
protected override void Render(HtmlTextWriter writer)
{
// GridView
foreach (GridViewRow row in gvwDepartment.Rows)
{
if (row.RowState == DataControlRowState.Edit)
{ // 編輯狀態
row.Attributes.Remove("onclick");
row.Attributes.Remove("ondblclick");
row.Attributes.Remove("style");
row.Attributes["title"] = "編輯行";
continue;
}
if (row.RowType == DataControlRowType.DataRow)
{
// 單擊事件,為了響應雙擊事件,需要延遲單擊響應,根據需要可能需要增加延遲
// 獲取ASP.NET內建回髮指令碼函式,返回 __doPostBack(<<EventTarget>>, <<EventArgument>>)
// 可直接硬編碼寫入指令碼,不推薦
row.Attributes["onclick"] = String.Format("javascript:setTimeout(\"if(dbl_click){{dbl_click=false;}}else{{{0}}};\", 1000*0.3);", ClientScript.GetPostBackEventReference(gvwDepartment, "Select$" + row.RowIndex.ToString(), true));
// 雙擊,設定 dbl_click=true,以取消單擊響應
row.Attributes["ondblclick"] = String.Format("javascript:dbl_click=true;window.location.href='UpdateDepartment.aspx?DepartmentID={0}';", gvwDepartment.DataKeys[row.RowIndex].Value.ToString());
//
row.Attributes["style"] = "cursor:pointer";
row.Attributes["title"] = "單擊選擇行,雙擊開啟詳細頁面";
}
}
base.Render(writer);
}
view plaincopy to clipboardprint?
protected void gvwDepartment_SelectedIndexChanged(object sender, EventArgs e)
{//設定選中行的顏色
gvwDepartment.SelectedRowStyle.BackColor = Color.FromArgb(222, 110, 222, 210);
}
protected void gvwDepartment_RowCreated(object sender, GridViewRowEventArgs e)
{//將GRIDVIEW的第一列,即選擇列隱藏
e.Row.Cells[0].Attributes.Add("style", "display:none;");
}
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/bnmjstu/archive/2009/07/17/4356430.aspx
功能: 單擊選中行,雙擊開啟詳細頁面
說明:
單擊事件(onclick)使用了 setTimeout 延遲,根據實際需要修改延遲時間
當雙擊時,通過全域性變數 dbl_click 來取消單擊事件的響應
常見處理行方式會選擇在 RowDataBound/ItemDataBound 中處理,這裡我選擇 Page.Render 中處理,至少基於以下考慮
1、RowDataBound 僅僅在呼叫 DataBind 之後才會觸發,回發通過 ViewState 建立空件不觸發 假如需要更多的處理,你需要分開部分邏輯到 RowCreated 等事件中
2、並且我們希望使用 ClientScript.GetPostBackEventReference 和 ClientScript.RegisterForEventValidation 方法 進行安全指令碼的註冊,而後者需要在頁的 Render 階段中才能處理
注意:需要在前臺加入javascript
<script type="text/javascript">
// 輔助全域性變數,指示是否雙擊
var dbl_click = false;
</script>
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/bnmjstu/archive/2009/07/17/4356430.aspx