- 浏览: 2574058 次
- 性别:
- 来自: 上海
-
文章分类
最新评论
-
coosummer:
推荐使用http://buttoncssgenerator.c ...
几个漂亮的Button的CSS -
sws354:
多表连接的方式效率更低,几百条数据就能看出效果。
MySQL随机取数据最高效率的方法 -
lsj111:
用java实现如何做
PHP中利用mysql进行访问统计的思路和实现代码 -
check-枫叶:
您好!我现在也在弄google Maps,现在可以把2点之间的 ...
Google Maps API用法教程 -
nothingtalk:
博主,您好。
“所以我们接下来要介绍除去Gideon Ehr ...
排列组合算法1:生成全部有序列b
在DataGrid中选择,确认,删除多行复选框列表
Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid (i.e. HotMail & Yahoo)
Introduction
Although I don't have either a Hotmail or Yahoo email account, I do have friends that do, and I have often seen them check their e-mail and noticed how it was all neatly displayed in a table. Right next to each item in their e-mail table was a simple little checkbox. At the bottom or top of this table you would have another checkbox giving them the ability to select all of the e-mail, and a button to submit back. Once this was done they would receive a nice popup alert to confirm what's about to happen, and then once this is OK' ed, boom, all the selected items are deleted.
.NET, of course, has the DataGrid, their finest and most popular control to date, and it should be pretty easy to get the same functionality, right? Well, not quite. Doing this with .NET is not that straightforward and functional as I would've liked it to be. Sure it's quite easy to add an edit / update button or link to each row and even a delete button to each, alongside a pop up alert as well! However, applying this delete feature to each and every button and deleting them one by one is a little maddening. Nor is the DataGrid set up in allowing a way of adding a main "select all" checkbox to easily select all of the boxes, and then applying any particular action.
In this article, we will examine how to create a fully functional DataGrid with all the features you'd find set up on Hotmail or Yahoo. As an added bonus we'll be performing all of our data tasks strictly utilizing Microsoft's new Data Access Application Block or DAAB v2. To any who may feel a little behind with DAAB, have no fear, everything here can still be accomplished with pre-DAAB data objects as the only difference here is the data access part of it. Trust me there is a huge difference between the two, for one DAAB enable you to write about 75% less code that you would normally need when compared with regular ADO.NET!
So, before we begin, download the DAAB dll from the above link, install it, and copy it into you application’s bin folder and you're ready to go. Also, be sure and take a peek at the DAAB documentation that came with your installation for an overview and any questions you may have.
Ok, let's get to it then.
Our fully-featured DataGrid
Selecting & deleting multiple items will definitely be set up quite differently than any other type of .NET DataGrid deleting you probably have seen. However, we'll still follow the same logical flow of deletion, and we'll still confirm any delete actions about to take place after we have selected all our items. Much of the magic in making this work is really going to come from client-side JavaScript, that is ultimately responsible for wiring up our main "select all" checkbox to handle the selecting and deselecting of our checkboxes. Also, included is our server-side delete method that erases our data, and a DataGrid refresher method to rebind our DataGrid after we have completed our data deletion.
Have a look at Figure 1 to get an idea of what your DataGrid will look like:
Figure 1
Here is the code to set up our DataGrid:
Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid (i.e. HotMail & Yahoo)
|
The code listed above is what makes our DataGrid set up behave just like the grids on Hotmail and Yahoo. Our .NET DataGrid will have the same functionality and options available for selecting however many items, or perhaps all, that you'd like to delete, and once you do and submit, kiss them goodbye.
DataGrid Setup
The first step towards creating this functionality is to begin by adding a new DataGrid column (either before or after our main data, it doesn't matter) that'll contain our individual checkboxes, but also include our main top "select all" checkbox (if and when we need to select and deselect all the checkboxes below it). To do this we first make sure our DataGrid's AutoGenerateColumns property is set to false, since we're going to create the necessary custom Header and Item Templates, alongside our necessary BoundColumns, within our DataGrid Columns section (that can be customized and could contain whatever you need them to).
The only important thing to notice here, regarding our objective, is our first TemplateColumn that contains our checkboxes. The main focal point with this whole application rests solely on this first Template Column and its two Sub Templates: HeaderTemplate and ItemTemplate. Each contain the necessary checkboxes, both are wired to onClick events that executes select_deselectAll (our super-slick client-side JavaScript method) when either checkbox is checked, and in turn are fully responsible for giving us all the great functionality I've spoken of. Incidentally, the WebDings font's lowercase letter "a" give us our check symbol.
Selecting and De-Selecting our Checkboxes
Now that both checkboxes are wired to our multi-faceted JavaScript method, how is that one function going to determine the checkbox its dealing with, and the action it needs to carry out? Ah, here's how :-)
Our function select_deselectAll, listed below, accepts two arguments: the Checkbox's checked value, and its ID. Once this function is called, and its two arguments have been passed in, it'll begin looping through our form. Next, it begins performing some conditional checking utilizing JavaScript's indexOf method to locate the appropriate checkbox, and is based on both the values passed in, which it turn ultimately will give us one of several causes and effects:
- If the main "select all" checkbox is checked, it will select all of the DataGrid checkboxes
- If the main "select all" checkbox is unchecked, then all of the DataGrid checkboxes get unselected
- Finally, if after the main "select all" checkbox is selected and all of the DataGrid's checkboxes are all checked, any one of those checkboxes gets unchecked, then the main checkbox is also unchecked. This way we don't end up having our checkbox's logic behaving inconsistently or erratically.
function select_deselectAll (chkVal, idVal) {
|
Figure 2 shows you the effect of the JavaScript above interacting with the DataGrid when selecting the top main "select all" checkbox.
Figure 2
Now, aside from this function allowing a quick full selection, you also have the option of manually selecting as many checkbox items as you wish. Next comes the tricky part in how to determine which ones were selected, and how to confirm this the instant you submit the form, and prior to actual deletion.
Confirming Multiple Deletes
In this section, we'll examine how to confirm multiple deletes when we submit our form. Below in Figure 3 you can now see the alert confirmation after selecting a couple of items, and then submitting the form by press the "Delete Items" button. The alert takes place at any time you submit the form (as long as you have more than one checkbox selected).
Figure 3
Note that this confirmation will alert with all checkboxes selected or a just a few as shown. Pressing the Delete Items button with none selected will not prompt any alert. Here now is how we determine what checkboxes are actually checked.
The first thing we did was set up our Delete Button at the end of our DataGrid; just a regular asp server button. We also wired a server-side event to it - Delete Store - that, when confirmed, will delete the records:
But how does that pop-up alert confirmation appear? Well, that's the cool thing. We get this by adding the code listed below to our Button server control as soon as the page loads, in our Page_Load method, by locating it using the FindControl method and then adding to the button attributes, like so:
WebControl button = (WebControl) Page.FindControl("Confirm"); button.Attributes.Add ("onclick", "return confirmDelete (this.form);"); |
So, the second the page loads, it attached the Javascript handler to this button, and if you examine the HTML source code, the button afterwords, actually looks like this:
Cool huh? Now, the second this button is pressed, is when it can now trigger the client side JavaScript function below:
function confirmDelete (frm) {
} |
Ok, what happening here? Well, the JS function above is, for all intents and purposes, not that different from the previous JavaScript function - "select_deselectAll." Except, instead of determining if the main "select all" checkbox is checked, it actually checks to see whether if any of the DataGrid row checkboxes are checked. If so, it'll then, and only then, alert you with a confirmation to proceed onto either to delete or cancel.
Deleting Data
Now recall our asp:button above, and its default JavaScript onclick event handler attached on Page_Load. Aside from this we also notice it has another OnClick event (this one being server based) that gets raised when the button is clicked, rather pressed, that'll allow it to fire the server-side DeleteStore method to delete our data:
public void DeleteStore (Object sender, EventArgs e) {
} |
Since having wired the two client/server methods together, it's our JavaScript code that actually intercepts this button's call and goes first. If you confirm OK, then will the deleting server-side method execute, otherwise it'll cancel all events after that point and prevent anything from posting back.
Looking at the DeleteStore() method, you'll notice that it is actually does a few things. First, it set's up the string variable "dgIDs" that will hold all of our selected DataGrid IDs. Next, it loops through the DataGrid, and gathers all of the selected item ids that are based on the row's TemplateColumn ID, which is why I kept the ID control as a TemplateColumn and the rest BoundColumns as these types of controls do not support the ID property we need for referencing our data. After this, it will, upon verifying checked items, gather all the ids and assign them to our "dgIDs" variable, that'll be used with our SQL "deleteSQL" delete statement.
The "deleteSQL" delete statement uses the "WHERE IN" argument to perform the multiple deletes in one shot. Since we need to separate each id with a comma, you'll notice that in the loop I attach a comma after each collected item. This way we'll have all of our items clearly defined in our SQL. One problem however is that since we add on a comma after each collected item, the last one as well will include a tail-end comma and SQL won't like this. For example, once we loop through the DataGrid, gather up all of the selected items, and assign it to our delete string we could end up with something like this:
DELETE from Stores WHERE stor_id IN (2,4,6,7,) |
Notice the last comma; that's a no-no. To quickly and easily remedy this, we must remove the last comma, and we do this by pulling the substring we need from the "dgIDs" string using LastIndexOf (",") effectively removing the last comma, and properly formatting the delete statement for SQL, like so:
DELETE from Stores WHERE stor_id IN (2,4,6,7) |
Finally, DeleteStore proceeds to execute the query against the database. Incidentally, for those wondering why I have a conditional with BxsChkd? Well it's because if I don't initially select any items, I'm returned an error on Page_Load due to our SqlHelper having nothing initialized. Therefore, by do so, our DeleteStore method will remain silent, and happily waiting in the wings until it does get the actual go ahead.
So that's the crux of our DataGrid application, and technology behind doing multiple checkbox deletes a la Hotmail and Yahoo style. And on that note, here's all the code. Just have SQL Server ready, DAAB installed, then cut and paste the code below, and have fun.
Here's our main page code:
Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid (i.e. HotMail & Yahoo)
|
And our MultiDeleteDG.WebForm code-behind file - mDatagrid.aspx.cs:
using System; using System.Data; using System.Data.SqlClient; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; //Import DAAB dll namespace namespace MultiDeleteDG /// <summary><br>/// Selecting, Confirming & Deleting Multiple Checkbox Items In A DataGrid (i.e. HotMail & Yahoo)<br>/// Author: Dimitrios Markatos - dmarko1@aol.com<br>/// Date: 8/2003<br>/// </summary>
}//End Namespace |
Conclusion
Well, that's it. Pretty awesome, and there was sure a lot to grasp as this certainly was a fairly complex article; but look at what you can do with a DataGrid now? There's nothing preventing you from adding paging to it although you'll have to delete whatever you need per page before paging to the next, or you could also store all your selected values in View State or any of of state methods, then pull them from there at the end.
At any rate, .NET clearly demonstrates that its Framework and all included is simply the best once again. Period!
Until next time. Happy .NETing
相关推荐
在这个示例中,我们关注的是如何在`DataGrid`中添加复选框,使得用户能进行多选操作。`DataGrid`的复选框功能通常用于实现全选/反选所有行的功能,提高用户交互性。 首先,我们需要理解`DataGridTemplateColumn`。...
在单元格模板(CellTemplate)中,我们同样添加了一个复选框,用于每行的数据选择。 接下来,我们需要在后台代码中处理全选和取消全选的逻辑。在`HeaderCheckbox_Checked`和`HeaderCheckbox_Unchecked`事件处理器中...
根据给定的文件信息,我们可以总结出关于Flex中带复选框的DataGrid的知识点,主要涉及Flex框架下的DataGrid组件及其自定义扩展,具体包括CheckBoxColumn、CheckBoxHeader和CheckBoxRenderer这三个类的设计与实现。...
本主题将深入探讨如何实现用户通过点击`DataGridView`中的复选框来删除所选择的行,这一功能在数据管理界面中非常实用。我们将讨论相关API、事件处理以及实现方法。 1. **复选框列(CheckBoxColumn)** 要在`...
本资源包含的是关于在DataGrid中实现复选框功能的源代码,这对于需要在表格中进行多选操作的应用非常有用。 首先,我们来看`CheckBoxHeaderRenderer.as`,这个文件是DataGrid列头的自定义渲染器。在Flex中,渲染器...
在许多应用程序中,我们可能需要在数据网格中添加复选框列,以便用户可以选择一个或多个行。此外,提供一个全选/全不选的功能能极大提高用户体验。本教程将详细讲解如何在C#的DataGridView中实现这个功能。 首先,...
在实际应用中,我们经常需要在DataGrid的每一行中添加复选框(checkBox),以便用户可以选择一行或多行数据进行操作。这个教程将详细解释如何在Flex的DataGrid中动态添加复选框,并通过一个名为`DataGridDemo`的示例...
在网页设计中,表单元素如复选框(Checkbox)和单选按钮(Radio Button)是用户交互的重要组成部分。它们通常用于收集用户的选择性信息,例如用户同意服务条款、选择偏好或进行多选项决策。然而,原生的HTML表单元素...
Datagridview控件没有全选或取消全选的复选框,本资源用VB2015通过扩展DataGridViewColumnHeaderCell类,在列头绘制一个复选框checkbox控件,通过定义checkbox鼠标单击事件来实现行的全选或取消全选。checkbox还可以...
easyui datagrid 删除多行操作时会出现异常,该方法可以直接加载一次数据,避免该问题的发生
复选框可以添加到数据网格的列中,允许用户通过勾选或取消勾选来选择或取消选择特定的行。这在需要批量操作数据,如导出、删除或更新时特别有用。 实现AdvancedDataGrid中的复选框功能,开发者通常需要自定义数据项...
在实际应用中,我们经常需要在`DataGrid`中添加复选框(CheckBox)来实现行级别的选择功能,同时提供全选和非全选的功能。这个功能不仅方便用户批量操作,也为应用提供了更好的交互性。本教程将详细讲解如何在`WPF ...
本资源"vs2003用CheckBox实现DataGrid的多行删除源码.rar"提供了一种方法,通过在DataGrid中添加CheckBox控件来实现用户选择多行,然后一次性删除所选行的功能。以下是对这个技术点的详细解释: 1. **DataGrid控件*...
例如,我们可以创建一个JSON对象存储所有筛选条件,每次复选框状态改变时,更新这个对象,然后调用datagrid的`reload`方法重新加载数据。 在描述中提到的"200-300"和"500-600"这样的范围筛选,可以通过EasyUI的`...
VB 6.0 在DataGrid表格中实现下拉列表框,大家可以看截图,在如演示效果所示的DataGrid表格中,单击“单位”一列任意行数据,会显示出一个下拉框,供输入者选择单位,扩展了DataGrid的功能,增加了易用性,提升了...
### combogrid 本地搜索多列匹配及复选框选中问题 #### 一、问题背景与概述 在使用EasyUI框架中的`combogrid`组件时,可能会遇到两个主要问题:一是本地模糊搜索功能在多列上的实现;二是点击复选框时,无法正确...
我们需要确保在复选框的`Checked`或`Unchecked`事件中记录相应的行信息。 3. **数据绑定**:DataGrid通常与数据源(如DataTable、ObservableCollection等)绑定,以展示数据。每个数据项映射到DataGrid的一行,而列...
DataGrid控件是.net里面应用非常广泛的控件,在实际工作中,经常需要用户在输入数据的时候能够直接从几个备选数据进行选择,就是要嵌入下拉列表框,或者输入日期和时间,就是用到嵌入的DateTimePacker控件,还有使用...
这意味着我们需要在CellRenderer中监听复选框的事件,并通过冒泡机制将事件传递回DataGrid。在DataGrid内部,接收到这个事件后,检查是否所有行的复选框都已选中或未选中,然后更新表头复选框的状态。 7. **性能...
C#winform DataGridView checkbox复选框 实现单选效果,系统默认的是界面中多选,下午没事,做个单选的效果,同时复习下datagridview的常用知识,包含dantGridVIew的常用操作内容,注意事项,代码有点乱,学习。