Raise Server-Side Click Event of CheckBox in a DataGrid Template Column
In an ASP.Net web page, there is a GridView control to show data retrieved from the database. And there is a CheckBox control in the template column of the GridView. When user click on the CheckBox to select/unselect the row on the gridview, and highlight the selected rows.
Problem
You need to respond to the checkbox change event, and know which check box on which row fired the event. The checkbox does not automatically call the datagrid's ItemCommand, so how can we access the datagridItem that represents the row we're clicking?
Solution
In the page code behind file, create a Sub that matches the checkbox changed signature. It's important that the method be declared public or protected.
Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
End Sub
In the aspx page, modify the checkbox tag by adding the OnCheckedChanged attribute, and setting it to the name of the sub that you just created above. You'll also need autopostback set to true.<ItemTemplate>
<asp:CheckBox id="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="Check_Clicked">asp:CheckBox>
>
Now when the checkbox is clicked, the Check_Clicked event will be fired.
That's great, but the signature for the Checkbox_Clicked event does not give us immediate access to the datagridItem like the familiar ItemCommand signature does, so we've got a bit more to do.
It turns out that the checkbox's NamingContainer property evaluates to the datagridItem that we are seeking. Add the following code to the Check_Clicked procedure, and you're home free.
Protected Sub Check_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim ck1 As CheckBox = CType(sender, CheckBox)
Dim dgItem As DataGridItem = CType(ck1.NamingContainer, DataGridItem)
'now we've got what we need! Label1.Text = "You selected row " & dgItem.Cells(0).TextEnd Sub
References
http://geekswithblogs.net/sgreenberger/archive/2004/11/12/14871.aspx?Pending=true
[@more@]來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1044086/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Using the Template Collection to Add a Template Column in WebDataGridWeb
- easyui datagrid editor checkbox 選中UI
- 為DataGrid新增CheckBox控制元件 (轉)控制元件
- raise()AI
- 在DataGrid頁首上新增全選的CheckBox控制元件控制元件
- 利用Session紀錄datagrid模板列中CheckBox的狀態 (轉)Session
- WPF datagrid implement multi select via behavior selectionchanged event in MVVMMVVM
- arise、rise、raise、arouseAI
- DataGrid中通過CHECKBOX來修改庫裡某個欄位的值
- how to click "close" button on vb.net form without firing Textbox's Lostfoucs() eventORM
- Oracle 中的exception——(Raise)OracleExceptionAI
- signal raise alarm用法AI
- RAISE_APPLICATION_ERRORAIAPPError
- Oracle中RAISE異常OracleAI
- Swift Server-sideSwiftServerIDE
- JavaScript click 事件JavaScript事件
- jQuery click事件jQuery事件
- click事件生成事件
- Server-side vulnerabilities :path traversalServerIDE
- go text/template & Consul-templateGo
- $().click () 和 $(document).on ('click','要選擇的元素',function (){}) 的不同Function
- this.button1.Click += new System.EventHandler(this.button1_Click);
- Error: no such columnError
- html templateHTML
- WPF datagrid mvvm multi select via customize datagridMVVM
- ES 筆記十四:Index Template 和 Dynamic Template筆記Index
- c++ 模板模板引數("Template Template Parameters")C++
- Click: 命令列工具神器命令列
- click事件"穿透"問題事件穿透
- jQuery .click()方法傳參jQuery
- Column Monitoring
- go template使用Go
- BaiDu TemplateAI
- Django模板templateDjango
- Js template engineJS
- artTemplate js templateJS
- C_template
- Mendix Page Template