在上一篇中,我们已经开发了需要的CheckBox控件,其位于HughLin.dll中。在本篇我们将讲解如何使用该控件,进行删除多条记录功能。
首先,在你的网站的Web.config文件中配置对该控件的引用。
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<pages>
<controls>
<add namespace="HughLin.Web.UI.WebControls" tagPrefix="hlAsp" assembly="HughLin"/>
</controls>
</pages>
</system.web>在你的aspx文件中,使用如下:
<asp:LinkButton ID="DeleteUsers" runat="server" OnClientClick="if (AspNetV2_CheckHasData('UserCheckBoxGroup1')) {return confirm('您确定要删除所选用户吗?');};else {alert('您还未选择用户!');return false;}" OnClick="DeleteUsers_Click" CausesValidation="false">删除用户</asp:LinkButton>
在你的aspx.cs文件中实现删除方法DeleteUsers_Click
我在此使用的是强类型的DataSet:EmployeeData,该DataSet中有一个数据表Employee,该数据表包含列:UserName,RealName,Email。
你可以在App_Code文件夹中添加该数据集。并添加EmployeeBLL.cs文件,代码如下:
本程序使用了GotDotNet的DataAccess Application Block 3.0控件,该控件及其使用方法可以到www.gotdotnet.com去下载。
不过,在使用的过程,我发现如果PostBack后CheckBox控件的选择状态不能保存。美中不足,为此我改进了CheckBox控件。在下一篇中,我将发布该解决方案,这又将设计到另一个控件的开发。
首先,在你的网站的Web.config文件中配置对该控件的引用。
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<system.web>
<pages>
<controls>
<add namespace="HughLin.Web.UI.WebControls" tagPrefix="hlAsp" assembly="HughLin"/>
</controls>
</pages>
</system.web>
<asp:LinkButton ID="DeleteUsers" runat="server" OnClientClick="if (AspNetV2_CheckHasData('UserCheckBoxGroup1')) {return confirm('您确定要删除所选用户吗?');};else {alert('您还未选择用户!');return false;}" OnClick="DeleteUsers_Click" CausesValidation="false">删除用户</asp:LinkButton> 1
<asp:GridView ID="EmployeeGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="UserName"
2
DataSourceID="EmployeeGridDataSource">
3
<Columns>
4
<asp:TemplateField>
5
<HeaderTemplate>
6
<hlAsp:CheckBox ID="SelectAll" runat="server" Group="UserCheckBoxGroup1" IsParent="true"
7
Text="全选" />
8
</HeaderTemplate>
9
<HeaderStyle Width="50px" />
10
<ItemStyle Width="50px" />
11
<ItemTemplate>
12
<hlAsp:CheckBox ID="UserNameCheckBox" runat="server" Group="UserCheckBoxGroup1" BindedValue='<%# Eval("UserName") %>' />
13
</ItemTemplate>
14
</asp:TemplateField>
15
<asp:BoundField DataField="RealName" HeaderText="姓名" SortExpression="RealName">
16
<HeaderStyle Width="100px" />
17
<ItemStyle Width="100px" />
18
</asp:BoundField>
23
<asp:BoundField DataField="Email" HeaderText="电子信箱" SortExpression="Email">
24
<HeaderStyle Width="160px" />
25
<ItemStyle Width="160px" HorizontalAlign="left" />
26
</asp:BoundField>
27
</Columns>
28
</asp:GridView>
29
<asp:ObjectDataSource ID="EmployeeGridDataSource" runat="server" SelectMethod="GetAllEmployees"
30
TypeName="EmployeeBLL">
31
</asp:ObjectDataSource>
<asp:GridView ID="EmployeeGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="UserName"2
DataSourceID="EmployeeGridDataSource">3
<Columns>4
<asp:TemplateField>5
<HeaderTemplate>6
<hlAsp:CheckBox ID="SelectAll" runat="server" Group="UserCheckBoxGroup1" IsParent="true"7
Text="全选" />8
</HeaderTemplate>9
<HeaderStyle Width="50px" />10
<ItemStyle Width="50px" />11
<ItemTemplate>12
<hlAsp:CheckBox ID="UserNameCheckBox" runat="server" Group="UserCheckBoxGroup1" BindedValue='<%# Eval("UserName") %>' />13
</ItemTemplate>14
</asp:TemplateField>15
<asp:BoundField DataField="RealName" HeaderText="姓名" SortExpression="RealName">16
<HeaderStyle Width="100px" />17
<ItemStyle Width="100px" />18
</asp:BoundField>23
<asp:BoundField DataField="Email" HeaderText="电子信箱" SortExpression="Email">24
<HeaderStyle Width="160px" />25
<ItemStyle Width="160px" HorizontalAlign="left" />26
</asp:BoundField>27
</Columns>28
</asp:GridView>29
<asp:ObjectDataSource ID="EmployeeGridDataSource" runat="server" SelectMethod="GetAllEmployees"30
TypeName="EmployeeBLL">31
</asp:ObjectDataSource> 1
// 删除用户
2
protected void DeleteUsers_Click(object sender, EventArgs e)
3
{
4
EmployeeData employeeData = new EmployeeData();
5
6
// 获取被选中的用户名集合
7
foreach (GridViewRow row in EmployeeGrid.Rows)
8
{
9
TableCell cell = row.Cells[0];
10
HughLin.Web.UI.WebControls.CheckBox userNameCheckBox = (HughLin.Web.UI.WebControls.CheckBox)cell.FindControl("UserNameCheckBox");
11
if (userNameCheckBox.Checked)
12
{
13
string userName = userNameCheckBox.BindedValue;
14
EmployeeData.EmployeeRow dataRow = employeeData.Employee.NewEmployeeRow();
15
dataRow.UserName = userName;
16
employeeData.Employee.Rows.Add(dataRow);
17
dataRow.AcceptChanges();
18
dataRow.Delete();
19
}
20
}
21
bool flag = (new EmployeeBLL()).DeleteEmployee(employeeData);
22
if (flag)
23
{
24
EmployeeGrid.DataBind();
25
}
26
}
// 删除用户2
protected void DeleteUsers_Click(object sender, EventArgs e)3
{4
EmployeeData employeeData = new EmployeeData();5

6
// 获取被选中的用户名集合7
foreach (GridViewRow row in EmployeeGrid.Rows)8
{9
TableCell cell = row.Cells[0];10
HughLin.Web.UI.WebControls.CheckBox userNameCheckBox = (HughLin.Web.UI.WebControls.CheckBox)cell.FindControl("UserNameCheckBox");11
if (userNameCheckBox.Checked)12
{13
string userName = userNameCheckBox.BindedValue;14
EmployeeData.EmployeeRow dataRow = employeeData.Employee.NewEmployeeRow();15
dataRow.UserName = userName;16
employeeData.Employee.Rows.Add(dataRow);17
dataRow.AcceptChanges();18
dataRow.Delete();19
}20
}21
bool flag = (new EmployeeBLL()).DeleteEmployee(employeeData);22
if (flag)23
{24
EmployeeGrid.DataBind();25
}26
}你可以在App_Code文件夹中添加该数据集。并添加EmployeeBLL.cs文件,代码如下:
1
/// <summary>
2
/// 删除员工数据
3
/// </summary>
4
/// <param name="employeeData">员工数据集合</param>
5
/// <returns>删除是否成功</returns>
6
public bool DeleteEmployee(EmployeeData employeeData)
7
{
8
bool flag = false;
9
IDbTransaction tran = null;
10
string[] sourceColumns =
11
new string[] { employeeData.Employee.UserNameColumn.ColumnName };
12
try
13
{
14
AdoHelper adoHelper = AdoHelper.CreateHelper(DbUtil.DbProvider);
15
IDbCommand insertCommand = null;
16
IDbCommand updateCommand = null;
17
IDbConnection conn = adoHelper.GetConnection(DbUtil.ConnectionString);
18
conn.Open();
19
tran = conn.BeginTransaction();
20
IDbCommand deleteCommand = adoHelper.CreateCommand(conn, "DeleteEmployee", sourceColumns);
21
deleteCommand.Transaction = tran;
22
adoHelper.UpdateDataset(insertCommand, deleteCommand, updateCommand, employeeData, employeeData.Employee.TableName);
23
tran.Commit();
24
flag = true;
25
}
26
catch
27
{
28
try
29
{
30
if (tran != null)
31
{
32
tran.Rollback();
33
}
34
}
35
catch
36
{
37
}
38
}
39
return flag;
40
}
/// <summary>2
/// 删除员工数据3
/// </summary>4
/// <param name="employeeData">员工数据集合</param>5
/// <returns>删除是否成功</returns>6
public bool DeleteEmployee(EmployeeData employeeData)7
{8
bool flag = false;9
IDbTransaction tran = null;10
string[] sourceColumns = 11
new string[] { employeeData.Employee.UserNameColumn.ColumnName };12
try13
{14
AdoHelper adoHelper = AdoHelper.CreateHelper(DbUtil.DbProvider);15
IDbCommand insertCommand = null;16
IDbCommand updateCommand = null;17
IDbConnection conn = adoHelper.GetConnection(DbUtil.ConnectionString);18
conn.Open();19
tran = conn.BeginTransaction();20
IDbCommand deleteCommand = adoHelper.CreateCommand(conn, "DeleteEmployee", sourceColumns);21
deleteCommand.Transaction = tran;22
adoHelper.UpdateDataset(insertCommand, deleteCommand, updateCommand, employeeData, employeeData.Employee.TableName);23
tran.Commit();24
flag = true;25
}26
catch27
{28
try29
{30
if (tran != null)31
{32
tran.Rollback();33
}34
}35
catch36
{37
}38
}39
return flag;40
}不过,在使用的过程,我发现如果PostBack后CheckBox控件的选择状态不能保存。美中不足,为此我改进了CheckBox控件。在下一篇中,我将发布该解决方案,这又将设计到另一个控件的开发。

浙公网安备 33010602011771号