天天看點

ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid

功能:

  • 單擊行彈出目前行詳細頁面
  • 輕按兩下行進入編輯狀态(GridView/DataGrid内置 Edit)

    說明:

  • 單擊事件(onclick)使用了 setTimeout 延遲,根據實際需要修改延遲時間
  • 常見處理行方式會選擇在 RowDataBound/ItemDataBound 中處理,這裡我選擇 Page.Render 中處理,至少基于以下考慮
  • RowDataBound 僅僅在調用 DataBind 之後才會觸發,回發通過 ViewState 建立空件不觸發 假如需要更多的處理,你需要分開部分邏輯到 RowCreated 等事件中
  • 并且我們希望使用 ClientScript.GetPostBackEventReference 和 ClientScript.RegisterForEventValidation 方法 進行安全腳本的注冊,而後者需要在頁的 Render 階段中才能處理
  • 關于“DataGrid中采取的輔助按鈕支援回發”見ASP.NET DEMO8: 為 GridView 每行添加伺服器事件
  • PS:未實作 Edit 對應的 Update/Cancel ,根據需要自行添加即可。 

    原始需求:既有單擊又有輕按兩下的GridView是否存在(問了許多人都說不能,郁悶)

    可直接運作源碼(單頁 .aspx): 

    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    <% @ Page Language = " C# "   %>
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    <% @ Import Namespace = " System.Data "   %>
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    <%-- http: // community.csdn.net/Expert/TopicView3.asp?id=5767096--%>
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    <! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    < script runat = " server " >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         protected   void  Page_Load( object  sender, EventArgs e)
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            if (!IsPostBack) 
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                LoadGridViewProductData();
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                LoadDataGridProductData();
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         protected   void  GridView1_RowDataBound( object  sender, GridViewRowEventArgs e)
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        }    
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         protected   void  DataGrid1_ItemDataBound( object  sender, DataGridItemEventArgs e)
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            // 隐藏輔助按鈕列
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            int cellIndex = 0;
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            e.Item.Cells[cellIndex].Attributes["style"] = "display:none";
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        }    
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         void  LoadGridViewProductData()
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            DataTable dt = CreateSampleProductData();
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            GridView1.DataSource = dt;
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            GridView1.DataBind();    
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         void  LoadDataGridProductData()
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            DataTable dt = CreateSampleProductData();
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            DataGrid1.DataSource = dt;
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            DataGrid1.DataBind();
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         sample data #region sample data
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        static DataTable CreateSampleProductData()
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            DataTable tbl = new DataTable("Products");
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Columns.Add("ProductID", typeof(int));
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Columns.Add("ProductName", typeof(string));        
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Columns.Add("UnitPrice", typeof(decimal));
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Columns.Add("CategoryID", typeof(int));
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Rows.Add(1, "Chai", 18, 1);
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Rows.Add(2, "Chang", 19, 1);
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Rows.Add(3, "Aniseed Syrup", 10, 2);
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Rows.Add(4, "Chef Anton's Cajun Seasoning", 22, 2);
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Rows.Add(5, "Chef Anton's Gumbo Mix", 21.35, 2);
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Rows.Add(47, "Zaanse koeken", 9.5, 3);
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Rows.Add(48, "Chocolade", 12.75, 3);
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            tbl.Rows.Add(49, "Maxilaku", 20, 3);        
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            return tbl;
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        #endregion     
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         protected   void  GridView1_RowEditing( object  sender, GridViewEditEventArgs e)
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            GridView1.EditIndex = e.NewEditIndex;
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            LoadGridViewProductData();
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         protected   void  DataGrid1_EditCommand( object  source, DataGridCommandEventArgs e)
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            DataGrid1.EditItemIndex = e.Item.ItemIndex;
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            LoadDataGridProductData();
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         protected   override   void  Render(HtmlTextWriter writer)
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            // GridView
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            foreach (GridViewRow row in GridView1.Rows) 
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                if (row.RowState == DataControlRowState.Edit) 
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    { // 編輯狀态
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    row.Attributes.Remove("onclick");
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    row.Attributes.Remove("ondblclick");
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    row.Attributes.Remove("style");
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    row.Attributes["title"] = "編輯行";
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    continue;
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                if (row.RowType == DataControlRowType.DataRow) 
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    //單擊事件,為了響應輕按兩下事件,延遲 1 s,根據需要可能需要增加延遲
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    row.Attributes["onclick"] = String.Format("javascript:setTimeout(/"window.open('DummyProductDetail.aspx?productid={0}')/", 1000*1);event.cancelBubble=true;", GridView1.DataKeys[row.RowIndex].Value.ToString());
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    // 擷取ASP.NET内置回發腳本函數,傳回 __doPostBack(<<EventTarget>>, <<EventArgument>>)
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    // 可直接寫死寫入腳本,不推薦
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    row.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(GridView1, "Edit$" + row.RowIndex.ToString(), true);
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    //
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    row.Attributes["style"] = "cursor:pointer";
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    row.Attributes["title"] = "單擊打開詳細頁面、輕按兩下進入編輯";
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            // DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            foreach (DataGridItem item in DataGrid1.Items) 
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                if (item.ItemType == ListItemType.EditItem) 
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    item.Attributes.Remove("onclick");
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    item.Attributes.Remove("ondblclick");
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    item.Attributes.Remove("style");
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    item.Attributes["title"] = "編輯行";
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    continue;
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem) 
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    {
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    //單擊事件,為了響應輕按兩下事件,延遲 1 s,根據需要可能需要增加延遲
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    item.Attributes["onclick"] = String.Format("javascript:setTimeout(/"window.open('DummyProductDetail.aspx?productid={0}')/", 1000*1);event.cancelBubble=true;", DataGrid1.DataKeys[item.ItemIndex].ToString());
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    // 輕按兩下
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    // 擷取輔助的支援回發按鈕
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    // 相對而言, GridView 支援直接将 CommandName 作為 <<EventArgument>> 故不需要輔助按鈕
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    Button btnHiddenPostButton = item.FindControl("btnHiddenPostButton") as Button;
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    // 擷取ASP.NET内置回發腳本函數,傳回 __doPostBack(<<EventTarget>>, <<EventArgument>>)
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    // 可直接寫死寫入腳本,不推薦
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    item.Attributes["ondblclick"] = ClientScript.GetPostBackEventReference(btnHiddenPostButton, "");
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    //
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    item.Attributes["style"] = "cursor:pointer";
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                    item.Attributes["title"] = "單擊打開詳細頁面、輕按兩下進入編輯";
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
            base.Render(writer);
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
        }
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    </ script >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    < html xmlns = " http://www.w3.org/1999/xhtml "   >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    < head id = " Head1 "  runat = " server " >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         < title > ASP.NET DEMO15: GridView 行單擊與輕按兩下事件 </ title >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    </ head >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    < body >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         < form id = " form1 "  runat = " server " >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         < div >         
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < h3 > 功能: </ h3 >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 < li > 單擊彈出目前詳細頁面 </ li >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 < li > 輕按兩下進入編輯狀态(GridView / DataGrid内置 Edit) </ li >         
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < h3 > 說明: </ h3 >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 < li > 單擊事件(onclick)使用了 setTimeout 延遲,根據實際需要修改延遲時間 </ li >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 < li > 常見處理行方式會選擇在 RowDataBound / ItemDataBound 中處理,這裡我選擇 Page.Render 中處理,至少基于以下考慮
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                     < li style = " padding-left:20px; list-style-type:square " > RowDataBound 僅僅在調用 DataBind 之後才會觸發,回發通過 ViewState 建立空件不觸發
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                假如需要更多的處理,你需要分開部分邏輯到 RowCreated 等事件中 </ li >  
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                     < li style = " padding-left:20px; list-style-type:square " > 并且我們希望使用 
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                ClientScript.GetPostBackEventReference 和 ClientScript.RegisterForEventValidation 方法
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                進行安全腳本的注冊,而後者需要在頁的 Render 階段中才能處理 </ li >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 </ li >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 < li > 關于“DataGrid中采取的輔助按鈕支援回發”見 < a href = " http://www.cnblogs.com/Jinglecat/archive/2007/07/15/818394.html " > ASP.NET DEMO8: 為 GridView 每行添加伺服器事件 </ a >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < br  />
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < input type = " button "  id = " Button1 "  value = " Rebind "  onclick = " location.href=location.href; "   />
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < div style = " float:left " >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < h3 > GridView Version </ h3 >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < asp:GridView ID = " GridView1 "  DataKeyNames = " ProductID "  runat = " server "  AutoGenerateColumns = " False "  OnRowEditing = " GridView1_RowEditing "  OnRowDataBound = " GridView1_RowDataBound " >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 < Columns >                               
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                     < asp:TemplateField HeaderText = " ProductName "   >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                         < ItemTemplate >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                             <% # Eval( " ProductName " )  %>
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                         </ ItemTemplate >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                         < EditItemTemplate >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                             < asp:TextBox ID = " txtProductName "  runat = " server "  Text = ' <%# Bind("ProductName") %> '   />
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                         </ EditItemTemplate >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                     </ asp:TemplateField >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                     < asp:BoundField DataField = " UnitPrice "  HeaderText = " UnitPrice "   />
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 </ Columns >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             </ asp:GridView ></ div >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < div style = " float:left;padding-left:100px; " >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < h3 > DataGrid Version </ h3 >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             < asp:DataGrid ID = " DataGrid1 "  DataKeyField = " ProductID "   runat = " server "  AutoGenerateColumns = " False "  OnEditCommand = " DataGrid1_EditCommand "  OnItemDataBound = " DataGrid1_ItemDataBound " >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 < Columns >  
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                     < asp:TemplateColumn >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                         < ItemTemplate >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                             < asp:Button ID = " btnHiddenPostButton "  CommandName = " Edit "  runat = " server "  Text = " HiddenPostButton "  style = " display:none "   />
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                         </ ItemTemplate >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                     </ asp:TemplateColumn >           
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                     < asp:BoundColumn DataField = " ProductName "  HeaderText = " ProductName "   />
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                     < asp:BoundColumn DataField = " UnitPrice "  HeaderText = " UnitPrice "   />  
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
                 </ Columns >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             </ asp:DataGrid ></ div >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             </ li >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
             </ div >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
         </ form >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    </ body >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
    </ html >
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid
     效果:
    ASP.NET DEMO 15: 同時支援行單擊和輕按兩下事件的 GridView/DataGrid