Webforms DetailsView不保存编辑/更新数据

时间:2013-09-11 11:35:21

标签: asp.net data-binding binding webforms detailsview

用户进入此页面以编辑/更新特定员工的详细信息。员工详细信息显示在DetailsView中,该视图绑定到Northwind数据库。问题是当我单击编辑并更改某些数据然后单击更新 - 字段将返回到初始状态,就像没有任何更改一样。

Webform代码:

  <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();
        }
    }        
}

1 个答案:

答案 0 :(得分:0)

你应该只在初始加载时对DetailsView进行数据处理,而不是在每次回发时:

if(!IsPostBack)
{
    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();
    }
}
在事件发生之前触发

Page_Load,再次通过数据绑定来阻止事件被触发。