Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# “事件中的旧值收集”;“项目更新”;“详细信息”视图始终为空_C#_Asp.net_Webforms_Datasource_Detailsview - Fatal编程技术网

C# “事件中的旧值收集”;“项目更新”;“详细信息”视图始终为空

C# “事件中的旧值收集”;“项目更新”;“详细信息”视图始终为空,c#,asp.net,webforms,datasource,detailsview,C#,Asp.net,Webforms,Datasource,Detailsview,我正在使用DetailsView,但在更新时,我无法获取OldValue,因为ItemUpdate事件的DetailsViewUpdateEventArgs.OldValues始终为空。NewValues的值为ok 注意:我没有在我的应用程序中使用数据源组件(SqlDataSource、ObjectDataSource、EntityDataSource等)。在这种情况下,我应该手动执行某些操作吗 欢迎任何意见 谢谢 补充资料: 我正在使用ASP.NET4.0(WebForms) 下面是我认为相关

我正在使用DetailsView,但在更新时,我无法获取OldValue,因为ItemUpdate事件的DetailsViewUpdateEventArgs.OldValues始终为空。NewValues的值为ok

注意:我没有在我的应用程序中使用数据源组件(SqlDataSource、ObjectDataSource、EntityDataSource等)。在这种情况下,我应该手动执行某些操作吗

欢迎任何意见

谢谢

补充资料: 我正在使用ASP.NET4.0(WebForms)

下面是我认为相关的一段代码:

<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>
问题是,如果我想处理旧值,我不能:

该值的计算结果为0: int oldValuesCount=e.OldValues.Count

即使我将该值更改为新值,其计算结果也为null:
字符串companynameld=e.OldValues[“CompanyName”]作为字符串

似乎只有当数据源以声明方式绑定时,才会填充旧值

在这种情况下,您有两种变通方法:

  • 以编程方式在ViewState中保存可能需要的任何旧值,并在需要时检索它。这将保存到数据库的行程

  • 更好的选择是使用您在MoUpdate中定义的customer变量,因为它从数据库查询信息并包含所有旧值


  • 希望有帮助。

    你能发布你的代码吗?你最近怎么样?@MuhammadAkhtar,谢谢你的关注。我已经按照您的要求添加了新的补充信息。
        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();
    
        }