前台: < form id = " form1 " runat = " server " > < div > < asp:DataList ID = " dlEditItem " runat = " server " DataKeyField = " EmployeeID " OnCancelCommand = " dlEditItem_CancelCommand " OnEditCommand = " dlEditItem_EditCommand " OnUpdateCommand = " dlEditItem_UpdateCommand " > < HeaderTemplate > 人员信息 </ HeaderTemplate > < ItemTemplate > < asp:Button ID = " edit " runat = " server " Text = " Edit " CommandName = " Edit " /><% # Eval( " LastName " ) %><% # Eval( " FirstName " ) %> </ ItemTemplate > < EditItemTemplate > < asp:Label ID = " lastname " runat = " server " ><% # Eval( " LastName " ) %></ asp:Label > < asp:Label ID = " firstname " runat = " server " ><% #Eval( " FirstName " ) %></ asp:Label > < asp:TextBox ID = " title " runat = " server " Text = ' <%#Eval("Title") %> ' ></ asp:TextBox > < asp:Button ID = " update " CommandName = " update " runat = " server " Text = " Update " /> < asp:Button ID = " cancel " runat = " server " CommandName = " cancel " Text = " Cancel " /> </ EditItemTemplate > < FooterTemplate >< hr ></ FooterTemplate > </ asp:DataList ></ div > </ form > 后台: using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class DataListEditItem : System.Web.UI.Page ... { protected void Page_Load(object sender, EventArgs e) ...{ if (!IsPostBack) DataListDataBind(); } private void DataListDataBind() ...{ string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString; SqlConnection conn = new SqlConnection(connStr); SqlDataAdapter Adapter = new SqlDataAdapter("select employeeid,lastname, firstname, title from employees", conn); conn.Open(); DataSet ds = new DataSet(); try ...{ Adapter.Fill(ds, "testTable"); dlEditItem.DataSource = ds.Tables["testTable"].DefaultView; dlEditItem.DataBind(); } catch (Exception error) ...{ Response.Write(error.ToString()); } finally ...{ conn.Close(); } } protected void dlEditItem_CancelCommand(object source, DataListCommandEventArgs e) ...{ dlEditItem.EditItemIndex = -1; DataListDataBind(); } protected void dlEditItem_EditCommand(object source, DataListCommandEventArgs e) ...{ dlEditItem.EditItemIndex = e.Item.ItemIndex; DataListDataBind(); } protected void dlEditItem_UpdateCommand(object source, DataListCommandEventArgs e) ...{ int empID = (int)dlEditItem.DataKeys[e.Item.ItemIndex]; TextBox newTitle = (TextBox)e.Item.FindControl("title"); string strUpt = "update employees set title ='" + newTitle.Text + "' where employeeid=" + empID.ToString(); string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString; SqlConnection conn = new SqlConnection(connStr); SqlCommand cmd = new SqlCommand(strUpt, conn); conn.Open(); try ...{ cmd.ExecuteNonQuery(); dlEditItem.EditItemIndex = -1; DataListDataBind(); } catch(Exception error) ...{ Response.Write(error.ToString()); } finally ...{ conn.Close(); } }}