添加GridView/DataGrid單擊一行服務器事件_.Net教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:解析2個ASP.NET小技巧1. ASP.NET AJAX 中,如何用 JavaScript 調用服務器端的方法? 這里不是指調用簡單的PageMethod,因為靜態方法是不能操作當前頁面的控件的,所以靜態的PageMethod作用就跟普通的WebService一樣,比較局限。 那么,調用一般的服務器端方法,其實就是發起一個
實現功能:asp.net的GridView/DataGrid控件本身均支持行選擇事件(通過設置Button/LinkButton.CommandName="Selected",并在 SelectedIndexChanged 事件中處理)。
然而,有時候我們希望用戶點擊網頁上GridView/DataGrid 一行中任意位置都可以實現觸發一個事件,并在服務端對此行進行相應處理,現在我們就實現此功能。
實現方式:
這里我們采取的方法有點 "hack" :
通過客戶端 javascript 引發行中隱藏的按鈕(Button/LinkButton 均可以)的 click 事件。
主要代碼:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="ProductName" > <ItemTemplate> <%# Eval("ProductName") %> <asp:Button ID="btnHiddenPostButton" CommandName="HiddenPostButtonCommand" runat="server" Text="HiddenPostButton" style="display:none" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" /> </Columns> </asp:GridView> |
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button; if (btnHiddenPostButton != null) { e.Row.Attributes["onclick"] = String.Format("javascript:document.getElementById(’{0}’).click()", btnHiddenPostButton.ClientID); // 額外樣式定義 e.Row.Attributes["onmouseover"] = "javascript:this.style.background=’red’"; e.Row.Attributes["onmouseout"] = "javascript:this.style.background=’’"; e.Row.Attributes["style"] = "cursor:pointer"; e.Row.Attributes["title"] = "單擊選擇當前行"; } // 若希望將隱藏按鈕單獨放于一列,則設置此列隱藏,占位符 <cellIndex> 表示此列索引 //e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none"; } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int rowIndex = -1; GridViewRow row = null; switch (e.CommandName) { case "HiddenPostButtonCommand": // 模板列 Control cmdControl = e.CommandSource as Control; // 表示觸發事件的 IButtonControl,保持統一性并便于后續操作,我們這里直接轉化為控件基類 Control row = cmdControl.NamingContainer as GridViewRow; // 當前行 // 如何訪問單元格值 // string txt = row.Cells[0].Text; // 如何獲取模板列中的 Label // string lbl = row.FindControl("MyLabelID") as Label; // 執行更多的自定義操作 // .... // ..... Response.Write(String.Format("GridView Version 當前第 {0} 行:", row.RowIndex + 1)); break; // case "Command2": // more cases // ..... } } |
![](http://p1.mb5u.com/allimg/100529/13511CG8-0.gif)
分享:解析asp.net編程中6條實用語句1.Panel橫向滾動,縱向自動擴展 <asp:panelstyle=quot;overflow-x:scroll;overflow-y:auto;quot;></asp:panel> 2.回車轉換成Tab (1) <scriptlanguage=quot;javascriptquot;for=quot;documentquot;event=quot;onkeydownquot;> if(event.keyCode==13amp;
相關.Net教程:
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發送Email實例(可帶附件)
- js實現廣告漂浮效果的小例子
- asp.net Repeater 數據綁定的具體實現
- Asp.Net 無刷新文件上傳并顯示進度條的實現方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數據庫(連接字符串的配置及獲取)
- asp.net頁面傳值測試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現分頁示例代碼
- 相關鏈接:
- 教程說明:
.Net教程-添加GridView/DataGrid單擊一行服務器事件
。