用户来到此页面以编辑/更新特定员工的详细信息。员工详细信息显示在与 Northwind 数据库绑定的 DetailsView 中。问题是当我单击编辑并更改一些数据然后单击更新时 - 字段返回到初始状态,就好像没有任何变化一样。
网络表单代码:
<form id="form1" runat="server">
<asp:DetailsView ID="DetailsViewEditEmployee"
runat="server"
AutoGenerateRows="False"
Height="50px"
Width="314px"
OnItemCommand="DetailsViewEditEmployee_ItemCommand"
OnModeChanging="DetailsViewEditEmployee_ModeChanging"
OnItemUpdating="DetailsViewEditEmployee_ItemUpdating"
OnItemUpdated="DetailsViewEditEmployee_ItemUpdated" >
<Fields>
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False" ReadOnly="False" SortExpression="EmployeeID" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:CommandField ButtonType="Button" CausesValidation="False" ShowEditButton="True" />
</Fields>
</asp:DetailsView>
CustomerID:
<asp:Literal Text="text" runat="server" id="LiteralCustomerId" Mode="Encode" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" DeleteCommand="DELETE FROM [Employees] WHERE [EmployeeID] = @EmployeeID" InsertCommand="INSERT INTO [Employees] ([LastName], [FirstName], [Title], [Address], [City]) VALUES (@LastName, @FirstName, @Title, @Address, @City)" SelectCommand="SELECT [EmployeeID], [LastName], [FirstName], [Title], [Address], [City] FROM [Employees]" UpdateCommand="UPDATE [Employees] SET [LastName] = @LastName, [FirstName] = @FirstName, [Title] = @Title, [Address] = @Address, [City] = @City WHERE [EmployeeID] = @EmployeeID">
<UpdateParameters>
<asp:Parameter Name="LastName" Type="String" />
<asp:Parameter Name="FirstName" Type="String" />
<asp:Parameter Name="Title" Type="String" />
<asp:Parameter Name="Address" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="EmployeeID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
</form>
后面的代码:
public partial class EmployeesEdit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["Id"] == null)
{
Response.Redirect("EmployeesRepeater.aspx");
}
this.LiteralCustomerId.Text = Request.Params["id"];
var id = int.Parse(Request.Params["id"]);
using (var context = new NorthwindEntities())
{
var employees = context.Employees;
var empToList = employees.ToList();
var current =
from emp in empToList
where emp.EmployeeID == id
select emp;
this.DetailsViewEditEmployee.DataSource = current;
this.DetailsViewEditEmployee.DataBind();
}
}
protected void DetailsViewEditEmployee_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
if (e.NewMode == DetailsViewMode.Edit)
{
this.DetailsViewEditEmployee.ChangeMode(e.NewMode);
this.DetailsViewEditEmployee.DataBind(); // add this and check
}
if (e.CancelingEdit)
{
this.DetailsViewEditEmployee.ChangeMode(DetailsViewMode.ReadOnly);
this.DetailsViewEditEmployee.DataBind(); // add this and check
}
}
protected void DetailsViewEditEmployee_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
for (int i = 0; i < e.NewValues.Count; i++)
{
if (e.NewValues[i] != null)
{
e.NewValues[i] = Server.HtmlEncode(e.NewValues[i].ToString());
}
}
}
protected void DetailsViewEditEmployee_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
this.DetailsViewEditEmployee.DataBind();
}
protected void DetailsViewEditEmployee_ItemCommand(object sender, DetailsViewCommandEventArgs e)
{
if (e.CommandName.Equals("New"))
{
this.DetailsViewEditEmployee.ChangeMode(DetailsViewMode.Insert);
this.DetailsViewEditEmployee.DataBind();
}
else if (e.CommandName.Equals("Edit"))
{
this.DetailsViewEditEmployee.ChangeMode(DetailsViewMode.Edit);
this.DetailsViewEditEmployee.DataBind();
}
}
}