Multi-select ASP.NET datagrid
2004-08-27 13:30
447 查看
Download source files - 3.72 Kb
Download demo project - 8.83 Kb
Download demo project for VS.NET 2003 - 15.8 Kb
Add Template column for CHECKBOXES for selection (Hotmail/Yahoo style)
Add client-side
Add server side
This function is used to have Hotmail style selection, it iterate through every check box on the form and then selects/deselects the checkboxes.
The only challenge left was to highlight and un-highlight the rows on selection and deselection. For which, I used
This was the client side story. So far so good. One may argue why not use the plain simple HTML checkbox control? The answer is ASP.NET server control has a viewstate and therefore posting a page retains the rows selection.
Download demo project - 8.83 Kb
Download demo project for VS.NET 2003 - 15.8 Kb
Introduction
ASP.NET datagrid is a very powerful and flexible control, however some features like multi-select is not natively available. This article shows how easily this functionality can be achieved with a few simple tricks.Background
One of the projects I was working on had a User Inteface requirement for multi-selection of grid rows. User wanted Hotmail or Yahoo style multi-selection facility along with highlighting the selection (hard part).Using the code
After testing and applying some javascript and grid related coding techinque, I came up with the following working solution.Add Template column for CHECKBOXES for selection (Hotmail/Yahoo style)
Add client-side
onclick()and javascript for checkboxes, to highlight and mark checked rows.
Add server side
CheckedChanged()event for preserving highlights. [because everytime on postback datagrid resets colors for the selection]
<Columns> <asp:TemplateColumn> <HeaderTemplate> <asp:CheckBox id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" AutoPostBack="false" ToolTip="Select/Deselect All" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox id="chkSelect" onclick="javascript:HighlightRow(this);" runat="server"OnCheckedChanged= "grdEmployees_CheckedChanged" AutoPostBack="false" /> </ItemTemplate> </asp:TemplateColumn> </Columns>
SelectAllCheckBoxes()
This function is used to have Hotmail style selection, it iterate through every check box on the form and then selects/deselects the checkboxes.HighlightRow()
The only challenge left was to highlight and un-highlight the rows on selection and deselection. For which, I used HighlightRow()function, please note one very important thing when using
<asp:CheckBox>control. It surrounds
<SPAN>tags around
CHECKBOXand therfore in javascript you have to get the children of the
<SPAN>tag.
//------------------------------------------------------------- // Select all the checkboxes (Hotmail style) //------------------------------------------------------------- function SelectAllCheckboxes(spanChk){ // Added as ASPX uses SPAN for checkbox var oItem = spanChk.children; var theBox=oItem.item(0) xState=theBox.checked; elm=theBox.form.elements; for(i=0;i<elm.length;i++) if(elm[i].type=="checkbox" && elm[i].id!=theBox.id) { //elm[i].click(); if(elm[i].checked!=xState) elm[i].click(); //elm[i].checked=xState; } } //------------------------------------------------------------- //----Select highlish rows when the checkboxes are selected // // Note: The colors are hardcoded, however you can use // RegisterClientScript blocks methods to use Grid's // ItemTemplates and SelectTemplates colors. // for ex: grdEmployees.ItemStyle.BackColor OR // grdEmployees.SelectedItemStyle.BackColor //------------------------------------------------------------- function HighlightRow(chkB) { var oItem = chkB.children; xState=oItem.item(0).checked; if(xState) {chkB.parentElement.parentElement.style.backgroundColor='lightcoral'; // grdEmployees.SelectedItemStyle.BackColor chkB.parentElement.parentElement.style.color='white'; // grdEmployees.SelectedItemStyle.ForeColor }else {chkB.parentElement.parentElement.style.backgroundColor='white'; //grdEmployees.ItemStyle.BackColor chkB.parentElement.parentElement.style.color='black'; //grdEmployees.ItemStyle.ForeColor } } // -->
This was the client side story. So far so good. One may argue why not use the plain simple HTML checkbox control? The answer is ASP.NET server control has a viewstate and therefore posting a page retains the rows selection.
Server Side
Now, On the server side we have to make sure the highlights are intact, because on every postback ASP.NET renders grid and loses the highlights. Following method is used for re-rendering the highlights.Public Sub grdEmployees_CheckedChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Dim chkTemp As CheckBox = CType(sender, CheckBox) Dim dgi As DataGridItem dgi = CType(chkTemp.Parent.Parent, DataGridItem) If (chkTemp.Checked) Then dgi.BackColor = grdEmployees.SelectedItemStyle.BackColor dgi.ForeColor = grdEmployees.SelectedItemStyle.ForeColor Else dgi.BackColor = grdEmployees.ItemStyle.BackColor dgi.ForeColor = grdEmployees.ItemStyle.ForeColor End If End Sub
Getting your selection
Its easy! Iterate through theDataGridItemscollection and grab the checkbox [for ex.
DemoGridItem.Cells(0).Controls(1)]. And verify its
CHECKEDproperty. Oh, also you can use
DataKeyFieldof the dataset to grab specific datarows. Check out the attached code and you will love to find out how easy it is to multi-select rows.
Conclusion
Follow the downloaded code, you can simply use any SQL database. This is my very first article, hope .NET lovers would like it. Feedback is welcome.相关文章推荐
- Multi-select ASP.NET datagrid(DataGrid多条记录选择的实现)
- Developing a Multi-Select ASP.NET GridView using JQuery
- Multi-row Editing in the ASP.NET DataGrid...
- 【Jqurey EasyUI+Asp.net】----DataGrid数据绑定,以及增、删、改(SQL)
- asp.net总结之Repeater,DataList和DataGrid
- 让Asp.NET的DataGrid可排序、可选择、可分页
- asp.net中将DataGrid的内容导出为excel文件
- Asp.Net下的DataGrid的多层表头
- 为 ASP.NET Datagrid 创建自定义列.VB
- ASP.NET中为DataGrid添加合计字段
- Implement Custom Paging in the ASP.Net Datagrid Control...
- asp.net中DataGrid双行跨列表头设计心得!
- Asp.Net下的DataGrid的多层表头 (转)很简单的技巧
- ASP.Net开发者常见 Datagrid 错误
- ASP.NET中DataGrid加序号(转自NinetyNine的个人财富)
- DropDownList Controls In an ASP.Net DataGrid
- ASP.NET中DataGrid鼠标经过感知以及点击行弹出窗口
- ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页
- 为 ASP.NET Datagrid 创建自定义列
- (转)动态管理ASP.NET DataGrid数据列