如何处理批量的数据,将是每个从事WEB开发的人所面临的问题。现在web 开发对这些的解决方案并不是太好,例如AJAX很好的提高了用户的体验,但是不可否认,运用Ajax技术不同程度上会造成速度的缓慢。
如果是批量的大量数据处理我一直不太认为对于大型的数据处理使用Ajax。
言归正传,我们如何提高用户操作的方便性,就像OFFICE一样处理我们的web页面。Matt Berseth提供了一个解决方案。
首先我们需要建立Customer类如下:

Code

public int ID
{ get; set; }

public string FirstName
{ get; set; }

public string LastName
{ get; set; }

public string Address
{ get; set; }

public string City
{ get; set; }

public string State
{ get; set; }
public bool IsValid


{
get


{
return
!string.IsNullOrEmpty(this.FirstName) ||
!string.IsNullOrEmpty(this.LastName) ||
!string.IsNullOrEmpty(this.Address) ||
!string.IsNullOrEmpty(this.City) ||
!string.IsNullOrEmpty(this.State);
}
}

然后创建类CustomerDAO
public CustomerDAO()


{
//
//TODO:
//
}
private List<Customer> Customers


{
get


{
List<Customer> customers =
HttpContext.Current.Session["Customers"] as List<Customer>;
if (customers == null)


{
customers = new List<Customer>();
for (int i = 0; i < 8; i++)


{

customers.Add(new Customer()
{ ID = (i + 1) });
}
// cache the list
HttpContext.Current.Session["Customers"] = customers;
}
return customers;
}

set
{ HttpContext.Current.Session["Customers"] = value; }
}
private List<Customer> PersistedCustomers


{
get


{
List<Customer> customers = HttpContext.Current.Session["PersistedCustomers"] as List<Customer>;
// load the customers on first access
if (customers == null)


{
customers = new List<Customer>();
// cache the list
HttpContext.Current.Session["PersistedCustomers"] = customers;
}
return customers;
}
}
[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<Customer> FindAll()


{
return this.Customers;
}
[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<Customer> FindAllPersistedCustomers()


{
return this.PersistedCustomers;
}
[DataObjectMethod(DataObjectMethodType.Update)]
public void Update(Customer newValues)


{
// simulate putting this record back into the database
Customer oldValues = this.Customers.Find(x => x.ID == newValues.ID);
// update the in-memory values
oldValues.FirstName = newValues.FirstName;
oldValues.LastName = newValues.LastName;
oldValues.Address = newValues.Address;
oldValues.City = newValues.City;
oldValues.State = newValues.State;
}
public void Save()


{
this.Customers.ForEach(delegate(Customer c)


{
if (c.IsValid)


{
c.ID = 1;
if (this.PersistedCustomers.Count > 0)


{
c.ID = this.PersistedCustomers.Last().ID + 1;
}
this.PersistedCustomers.Add(c);
}
});
this.Customers = null;
}

然后创建 页面代码如下:

Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<link type="text/css" rel="Stylesheet" href="datasheet.css" />
<style type="text/css">
.debuggrid

{
border:solid 1px #000;
font-family:lucida grande,arial,helvetica,sans-serif;
}
.debuggrid TH,.debuggrid TD

{
padding:2px;
}
</style>
<script type="text/javascript" >
function pageLoad(sender,args)

{

if(!args.get_isPartialLoad())
{
// when the user clicks the radio buttons,
// update the class
$addHandler(
$get('rdoOffice2007'),
'click',

Function.createDelegate(this, function()
{
onSkinChanged();
})
);
$addHandler(
$get('rdoOffice2003'),
'click',

Function.createDelegate(this, function()
{
onSkinChanged();
})
);
}
// make sure the correct skin is applied
onSkinChanged();
}

function onSkinChanged()
{
var customers = $get('customers');
var rdoOffice2007 = $get('rdoOffice2007');
var rdoOffice2003 = $get('rdoOffice2003');

if(rdoOffice2007.checked)
{
// add the 2007 css class
Sys.UI.DomElement.addCssClass(customers, 'excel2007');
}

else
{
// remove the 2007 css class (the 2003 skin is the default one)
Sys.UI.DomElement.removeCssClass(customers, 'excel2007');
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="scripManager" runat="server" />
<asp:ObjectDataSource
ID="odsCustomers" runat="server" TypeName="CustomerDAO" DataObjectTypeName="Customer"
SelectMethod="FindAll" UpdateMethod="Update">
</asp:ObjectDataSource>
<div id="customers" class="datasheet excel2007">
<div class="header">
<input id="rdoOffice2007" runat="server" type="radio" name="skin" value="excel2007" checked="true" title="Office 2007" />Office 2007
<input id="rdoOffice2003" runat="server" type="radio" name="skin" value="excel2003" />Office 2003
</div>
<div class="outer">
<div class="inner">
<asp:ListView ID="lv" runat="server" DataSourceID="odsCustomers">
<LayoutTemplate>
<table class="datatable" cellpadding="0" cellspacing="0">
<tr>
<th class="first">#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>City</th>
<th>State</th>
</tr>
<tr id="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<th class="first">
<asp:Label ID="lblID" runat="server"
Text='<%# Bind("ID") %>' />
</th>
<td>
<asp:TextBox ID="txtFirstName" runat="server"
Text='<%# Bind("FirstName") %>' />
</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"
Text='<%# Bind("LastName") %>' />
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server"
Text='<%# Bind("Address") %>' />
</td>
<td>
<asp:TextBox ID="txtCity" runat="server"
Text='<%# Bind("City") %>' />
</td>
<td>
<asp:TextBox ID="txtState" runat="server"
Text='<%# Bind("State") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
</div>
<div class="footer">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</div>
</div>
<br />
<h2>Records Saved So Far
</h2>
<asp:ListView ID="lvDebug" runat="server">
<LayoutTemplate>
<table class="debuggrid" cellpadding="0" cellspacing="0">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
<th>City</th>
<th>State</th>
</tr>
<tr id="itemPlaceholder" runat="server" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<th><%# Eval("ID") %></th>
<td><%# Eval("FirstName") %></td>
<td><%# Eval("LastName") %></td>
<td><%# Eval("Address") %></td>
<td><%# Eval("City") %></td>
<td><%# Eval("State") %></td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>None</EmptyDataTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>

后体代码为:
protected void Page_Load(object sender, EventArgs e)


{
}
protected void btnSubmit_Click(object sender, EventArgs e)


{
// move the data from the controls
// back to our data object
foreach (ListViewDataItem i in this.lv.Items)


{
this.lv.UpdateItem(i.DataItemIndex, true);
}
// save the changes to the database
new CustomerDAO().Save();
}
protected void Page_PreRender(object sender, EventArgs e)


{
this.lvDebug.DataSource = new CustomerDAO().FindAllPersistedCustomers();
this.lvDebug.DataBind();
}

(英文 http://mattberseth.com/blog/2008/05/bulk_inserting_data_with_the_l.html)