天天看點

上接擴充GridView控件(6) - 響應行的單擊事件和輕按兩下事件

4、重寫OnPreRender方法,注冊上面那段用戶端腳本

 /// <summary> 

                /// OnPreRender 

                /// </summary> 

                /// <param name="e"></param> 

                protected override void OnPreRender(EventArgs e) 

                { 

                        base.OnPreRender(e); 

                        if (!String.IsNullOrEmpty(RowClickButtonID) || !String.IsNullOrEmpty(RowDoubleClickButtonID)) 

                        { 

                                if (!Page.ClientScript.IsClientScriptBlockRegistered("jsClickAndDoubleClick")) 

                                { 

                                        Page.ClientScript.RegisterClientScriptBlock( 

                                                this.GetType(), 

                                                "jsClickAndDoubleClick", JavaScriptConstant.jsClickAndDoubleClick 

                                                ); 

                                } 

                        } 

                }

5、重寫OnRowDataBound以實作資料行響應滑鼠的單擊和輕按兩下事件的功能。主要是給<tr>加上用戶端代碼,用來調用某個按鈕的click事件

/// <summary> 

                /// OnRowDataBound 

                protected override void OnRowDataBound(GridViewRowEventArgs e) 

                        if (e.Row.RowType == DataControlRowType.DataRow) 

                                if (!String.IsNullOrEmpty(RowClickButtonID) || !String.IsNullOrEmpty(RowDoubleClickButtonID)) 

                                        // GridViewRow的每個TableCell 

                                        foreach (TableCell tc in e.Row.Cells) 

                                        { 

                                                // TableCell裡的每個Control 

                                                foreach (Control c in tc.Controls) 

                                                { 

                                                        // 如果控件繼承自接口IButtonControl 

                                                        if (c.GetType().GetInterface("IButtonControl") != null && c.GetType().GetInterface("IButtonControl").Equals(typeof(IButtonControl))) 

                                                        { 

                                                                if (!String.IsNullOrEmpty(RowClickButtonID)) 

                                                                { 

                                                                        // 該按鈕的ID等于單擊行所對應的按鈕ID 

                                                                        if (c.ID == RowClickButtonID) 

                                                                        { 

                                                                                // 增加行的單擊事件,調用用戶端腳本,根據所對應按鈕的ID執行所對應按鈕的click事件 

                                                                                e.Row.Attributes.Add("onclick", "javascript:yy_RowClick('" + c.ClientID + "')"); 

                                                                        } 

                                                                } 

                                                                if (!String.IsNullOrEmpty(RowDoubleClickButtonID)) 

                                                                        // 該按鈕的ID等于輕按兩下行所對應的按鈕ID 

                                                                        if (c.ID == RowDoubleClickButtonID) 

                                                                                // 增加行的輕按兩下事件,調用用戶端腳本,根據所對應按鈕的ID執行所對應按鈕的click事件 

                                                                                e.Row.Attributes.Add("ondblclick", "javascript:yy_RowDoubleClick('" + c.ClientID + "')"); 

                                                        } 

                                                } 

                                        } 

                        base.OnRowDataBound(e); 

控件使用

添加這個控件到工具箱裡,然後拖拽到webform上,要實作行的單擊事件則設定RowClickButtonID為行單擊事件所對應的按鈕的ID,要實作行的輕按兩下事件則設定RowDoubleClickButtonID為行輕按兩下事件所對應的按鈕的ID。

ObjData.cs

using System; 

using System.Data; 

using System.Configuration; 

using System.Web; 

using System.Web.Security; 

using System.Web.UI; 

using System.Web.UI.WebControls; 

using System.Web.UI.WebControls.WebParts; 

using System.Web.UI.HtmlControls; 

using System.ComponentModel; 

/// OjbData 的摘要說明 

/// </summary> 

public class OjbData 

        public OjbData() 

        { 

                // 

                // TODO: 在此處添加構造函數邏輯 

        } 

        [DataObjectMethod(DataObjectMethodType.Select, true)] 

        public DataTable Select() 

                DataTable dt = new DataTable(); 

                dt.Columns.Add("no", typeof(string)); 

                dt.Columns.Add("name", typeof(string)); 

                for (int i = 0; i < 30; i++) 

                        DataRow dr = dt.NewRow(); 

                        dr[0] = "no" + i.ToString().PadLeft(2, '0'); 

                        dr[1] = "name" + i.ToString().PadLeft(2, '0'); 

                        dt.Rows.Add(dr); 

                } 

                return dt; 

}

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server"> 

        <title>SmartGridView測試</title> 

</head> 

<body> 

        <form id="form1" runat="server"> 

                <yyc:SmartGridView ID="SmartGridView1" runat="server" AutoGenerateColumns="False" 

                        DataSourceID="ObjectDataSource1" RowClickButtonID="btnTestRowClick" RowDoubleClickButtonID="btnTestRowDoubleClick"> 

                        <Columns> 

                                <asp:BoundField DataField="no" HeaderText="序号" SortExpression="no" ItemStyle-Width="100px" /> 

                                <asp:BoundField DataField="name" HeaderText="名稱" SortExpression="name" ItemStyle-Width="100px" /> 

                                <asp:TemplateField> 

                                        <footerstyle cssclass="hidden" /> 

                                        <headerstyle cssclass="hidden" /> 

                                        <itemstyle cssclass="hidden" /> 

                                        <itemtemplate> 

                                                <asp:Button id="btnTestRowClick" runat="server" CommandName="RowClick" CommandArgument='<%# Container.DataItemIndex %>' /> 

                                                <asp:Button id="btnTestRowDoubleClick" runat="server" CommandName="RowDoubleClick" CommandArgument='<%# Container.DataItemIndex %>' /> 

                                        </itemtemplate> 

                                </asp:TemplateField> 

                        </Columns> 

                </yyc:SmartGridView> 

                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Select" 

                        TypeName="OjbData"></asp:ObjectDataSource> 

        </form> 

</body> 

</html>

/*測試版的實作 結束*/

OK

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/345522,如需轉載請自行聯系原作者

繼續閱讀