事件&QUOT OldValues​​收集; ItemUpdating" DetailsView控件始终是空的 [英] OldValues collection in event "ItemUpdating" of DetailsView is always empty

查看:269
本文介绍了事件&QUOT OldValues​​收集; ItemUpdating" DetailsView控件始终是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm使用一个DetailsView,但更新的时候,我不能得到OldValues​​因为ItemUpdating事件的DetailsViewUpdateEventArgs.OldValues​​总是空空的。该NewValues​​有值确定。

I´m using a DetailsView but when updating, I cannot get the OldValues because the DetailsViewUpdateEventArgs.OldValues of ItemUpdating event is always empty. The NewValues has the values ok.

请注意:I'm在我的应用程序(的SqlDataSource,ObjectDataSource控件,EntityDataSource等)不使用数据源组件。在这种情况下,我应该做手工的东西吗?

Note: I´m not using a datasource component in my application (SqlDataSource, ObjectDataSource, EntityDataSource, etc.). In this case should I do something manually?

任何想法将受到欢迎!

谢谢!

I'm(的WebForms)

I´m using ASP.NET 4.0 (WebForms)

code的片段,我认为是相关如下:

A snippet of code that I believe to be relevant below:

<asp:DetailsView ID="customerDetails" runat="server" AutoGenerateRows="False" EmptyDataText="No data..." 
    meta:resourcekey="customerDetails" 
    onitemdeleting="customerDetails_ItemDeleting" 
    oniteminserting="customerDetails_ItemInserting" 
    onitemupdating="customerDetails_ItemUpdating" 
    onmodechanging="customerDetails_ModeChanging" 
    CssClass="customerDetails" DataKeyNames="CustomerID">
    <FooterTemplate>
        <asp:LinkButton ID="lnkNew" Text="New" ToolTip="New Customer" CommandName="New" meta:resourcekey="lnkNew" runat="server" />
    </FooterTemplate>
    <EmptyDataTemplate>
        <p><asp:Label ID="lblNoDataHasBeenFound" Text="No data has been found." meta:resourcekey="lblNoDataHasBeenFound" runat="server" /></p>
        <asp:LinkButton ID="lnkNew" Text="New" ToolTip="New Customer" CommandName="New" meta:resourcekey="lnkNew" runat="server" />
    </EmptyDataTemplate>
    <Fields>
        <%--CustomerID--%>
        <asp:TemplateField HeaderStyle-CssClass="detailsHeader" ItemStyle-CssClass="detailsField">
            <HeaderTemplate>
                <asp:Label ID="ltrCustomerIdLabel" meta:resourcekey="ltrCustomerId" Text="CustomerID:" runat="server" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="lblCustomerId" Text='<%# Eval("CustomerID") %>' runat="server"  />
            </ItemTemplate>
            <EditItemTemplate>
                <asp:Label ID="lblCustomerId" Text='<%# Eval("CustomerID") %>' runat="server"  />
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="txtCustomerId" Text='<%# Bind("CustomerID") %>' MaxLength="5" width="50px" runat="server" />
                <cc1:DataAnnotationsValidator ID="CustomerIdValidator" Type="String" runat="server" ControlToValidate="txtCustomerId" PropertyName="CustomerID" Text="*" SourceType="DataLayerPOCO.Customer, DataLayerPOCO" CssError="validationError" Display="None"/>
            </InsertItemTemplate>
            <HeaderStyle HorizontalAlign="Right" />
            <ItemStyle HorizontalAlign="Left" />
        </asp:TemplateField>

        <%--CompanyName--%>
        <asp:TemplateField HeaderStyle-CssClass="detailsHeader" ItemStyle-CssClass="detailsField">
            <HeaderTemplate>
                <asp:Literal ID="ltrCompanyNameLabel" meta:resourcekey="ltrCompanyName" Text="Company Name:" runat="server" />
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Literal ID="ltrCompanyName" Text='<%# Eval("CompanyName") %>' runat="server"/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:TextBox ID="txtCompanyName" Text='<%# Bind("CompanyName") %>' MaxLength="40" Width="310px"
                    runat="server" />
                <cc1:DataAnnotationsValidator ID="CompanyNameValidator" Type="String" runat="server" ControlToValidate="txtCompanyName" PropertyName="CompanyName" Text="*" SourceType="DataLayerPOCO.Customer, DataLayerPOCO" CssError="validationError" Display="None"/>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="txtCompanyName" Text='<%# Bind("CompanyName") %>' MaxLength="40" Width="310px"
                    runat="server" />
                <cc1:DataAnnotationsValidator ID="CompanyNameValidator" Type="String" runat="server" ControlToValidate="txtCompanyName" PropertyName="CompanyName" Text="*" SourceType="DataLayerPOCO.Customer, DataLayerPOCO" CssError="validationError" Display="None"/>
            </InsertItemTemplate>
            <HeaderStyle HorizontalAlign="Right" />

        </asp:TemplateField>
        ...
        ...
        other fields goes here
        ...
        ...
        <%--Edit/Insert--%>
        <asp:TemplateField HeaderStyle-CssClass="detailsHeader" ItemStyle-CssClass="detailsField">
            <ItemTemplate>
                <asp:ImageButton ID="imgEdit" ImageUrl="~/img/pencil.png" meta:resourcekey="imgEdit" AlternateText="Edit" ToolTip="Edit Customer" CommandName="Edit" runat="server" CausesValidation="false"/>
                <asp:ImageButton ID="imgDelete" ImageUrl="~/img/delete.png" meta:resourcekey="imgDelete" AlternateText="Delete" ToolTip="Delete Customer" CommandName="Delete" OnClientClick="return confirmDelete();" runat="server" CausesValidation="false"/>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:ImageButton ID="imgUpdate" ImageUrl="~/img/accept.png" meta:resourcekey="imgUpdate" AlternateText="Update" ToolTip="Update Customer" CommandName="Update" runat="server" />
                <asp:ImageButton ID="imgCancel" ImageUrl="~/img/cancel.png" meta:resourcekey="imgCancel" AlternateText="Cancel" ToolTip="Cancel Edit" CommandName="Cancel" runat="server" CausesValidation="false"/>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:ImageButton ID="imgSave" ImageUrl="~/img/disk.png" meta:resourcekey="imgSave" AlternateText="Save" ToolTip="Save Customer Data" CommandName="Insert" runat="server" CausesValidation="true"/>
                <asp:ImageButton ID="imgCancelInsert" ImageUrl="~/img/cancel.png" meta:resourcekey="imgCancelInsert" AlternateText="Cancel" ToolTip="Cancel Insert" CommandName="Cancel" runat="server" CausesValidation="false"/>
            </InsertItemTemplate>
        </asp:TemplateField>
    <Fields>
</asp:DetailsView>

本次活动ItemUpdating是象下面这样:

The event ItemUpdating is like below:

    protected void customerDetails_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
    {
        repository = repository ?? new NorthwindRepositoryEF();

        string id = e.Keys["CustomerID"] as string;

        DataLayerPOCO.Customer customer = repository.GetCustomer(id);

        string companyName = (string)e.NewValues["CompanyName"];
        string contactName = (string)e.NewValues["ContactName"];
        string contactTitle = (string)e.NewValues["ContactTitle"];
        string address = (string)e.NewValues["Address"];
        string city = (string)e.NewValues["City"];
        string region = (string)e.NewValues["Region"];
        string postalCode = (string)e.NewValues["PostalCode"];
        string country = (string)e.NewValues["Country"];
        string phone = (string)e.NewValues["Phone"];
        string fax = (string)e.NewValues["Fax"];

        // Update Customer with the new data
        customer.CompanyName = companyName;
        customer.ContactName = contactName;
        customer.ContactTitle = contactTitle;
        customer.Address = address;
        customer.City = city;
        customer.Region = region;
        customer.PostalCode = postalCode;
        customer.Country = country;
        customer.Phone = phone;
        customer.Fax = fax;

        repository.UpdateCustomer(customer);
        repository.Save();
        //---
        customerDetails.ChangeMode(DetailsViewMode.ReadOnly);
        BindCustomerDetails();
        BindCustomersList();

    }

问题是,如果我要处理的旧值,我不能

The problem is that if I want to handle the old values, I can´t:

此,计算结果为0:
    INT oldValues​​Count = e.OldValues​​.Count;

this, evaluates to 0: int oldValuesCount = e.OldValues.Count;

和此,计算结果为空,即使我改变值到一个新的:
    字符串companyNameOld = e.OldValues​​ [公司名称]作为字符串;

and this, evaluates to null, even if I alter the value to a new one: string companyNameOld = e.OldValues["CompanyName"] as string;

推荐答案

看来,当你有一个数据源声明界旧值仅填充。

It seems the old values are only populated when you have a data source bounded declaratively.

您有两种解决方法在这种情况下:

You have two workarounds in this case:


  1. 保存任何旧值,您可能需要在ViewState中与程式设计,当你想要它恢复。这节省了访问数据库。

  1. Save any old values you may need in ViewState programtically and retrieve it when you want it. This save a trip to the database.

一个更好的选择是使用您在OnItemUpdating定义,因为它从数据库中查询信息,并包含所有的旧价值客户的变量。

A better option would to use the customer variable that you define in OnItemUpdating as it queries information from the database and contains all the old values.

希望它帮助。

这篇关于事件&QUOT OldValues​​收集; ItemUpdating&QUOT; DetailsView控件始终是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆