本文档是 Visual C# 教程 (切换到 Visual Basic 教程)

在本教程中,我们将研究如何将 ObjectDataSource 的 Insert()、Update() 和 Delete() 方法映射到 BLL 类的方法,以及如何配置 GridView、DetailsView 和 FormView 控件,使它们提供数据修改功能。

« 前一篇教程  |  下一篇教程 »

Part 1

简介

在前面的几篇教程中,我们学习了如何使用 GridView 、DetailsView 和FormView 控件在 ASP.NET 页面中显示数据。但这些控件只处理提供给它们的数据。通常,这些控件通过使用数据源控件(如 ObjectDataSource )访问数据。我们已经学习了 ObjectDataSource 是如何作为 ASP.NET 页面和基础数据之间的代理的。当一个 GridView 需要显示数据时,它将调用 ObjectDataSource 的 Select() 方法。后者随之从业务逻辑层 ( BLL ) 调用一个方法,该方法调用相应的数据访问层 ( DAL ) 的 TableAdapter 的方法。最后,TableAdapter 的方法向 Northwind 数据库发送一个 SELECT 查询。

回想一下,当我们在第一篇教程 中创建 DAL 中的 TableAdapter 时,Visual Studio 自动添加从基础数据库表插入、更新和删除数据的方法。此外,在 创建业务逻辑层教程中,我们在 BLL 中设计了一些方法,用于向下调用 DAL 层中的 这些数据修改 方法。

除Select() 方法之外,ObjectDataSource 还能提供Insert() 、Update() 和 Delete() 方法。这三个方法与 Select() 方法一样,都可以映射到基础对象中的方法。当将 GridView 、DetailsView 和 FormView 控件配置为插入、更新或删除数据时,这几个控件将提供一个用户界面用于修改基础数据。这个用户界面调用 ObjectDataSource 的 Insert() 、Update() 和 Delete() 方法,这些方法继而调用基础对象的相关方法(见图 1 )。

图1 :ObjectDataSource 的 Insert() 、Update() 和 Delete() 方法提供一个到 BLL 的代理

在本教程中,我们将研究如何将 ObjectDataSource 的 Insert() 、Update() 和 Delete() 方法映射到BLL 类的方法,以及如何配置 GridView 、DetailsView 和FormView 控件,使它们提供数据修改功能。

步骤1 :创建插入、更新和删除教程 网 页

开始学习插入、更新和删除数据之前,让我们先花些时间在我们的网站项目中创建本教程和后面几篇教程需要使用的ASP.NET 页面。首先,添加一个名为 EditInsertDelete 的新文件夹。接下来,将以下ASP.NET 页面添加到该文件夹,确保将每个页面关联到 Site.master 母版页:

  • Default.aspx
  • Basics.aspx
  • DataModificationEvents.aspx
  • ErrorHandling.aspx
  • UIValidation.aspx
  • CustomizedUI.aspx
  • OptimisticConcurrency.aspx
  • ConfirmationOnDelete.aspx
  • UserLevelAccess.aspx

 

图2 :为与数据修改相关的教程添加 ASP.NET 页面

与在其它文件夹中一样,EditInsertDelete 文件夹中的 Default.aspx 将列出这部分的相关教程。回想一下, SectionLevelTutorialListing.ascx 用户控件用于提供此功能。因此,我们从 Solution Explorer 中将此用户控件拖放到 Default.aspx 页面的 Design 视图中,从而将它添加到该页面。

图3 :将 SectionLevelTutorialListing.ascx 用户控件添加到 Default.aspx

最后,将这些页面作为条目添加到 Web.sitemap 文件中。具体地说,将以下标记添加到自定义格式设置<siteMapNode> 之后 :

<siteMapNode title="Editing, Inserting, and Deleting"
    url="~/EditInsertDelete/Default.aspx"
    description="Samples of Reports that Provide Editing, Inserting,
                  and Deleting Capabilities">
    <siteMapNode url="~/EditInsertDelete/Basics.aspx"
        title="Basics"
        description="Examines the basics of data modification with the
                      GridView, DetailsView, and FormView controls." />
    <siteMapNode url="~/EditInsertDelete/DataModificationEvents.aspx"
        title="Data Modification Events"
        description="Explores the events raised by the ObjectDataSource
                      pertinent to data modification." />
    <siteMapNode url="~/EditInsertDelete/ErrorHandling.aspx"
        title="Error Handling"
        description="Learn how to gracefully handle exceptions raised
                      during the data modification workflow." />
    <siteMapNode url="~/EditInsertDelete/UIValidation.aspx"
        title="Adding Data Entry Validation"
        description="Help prevent data entry errors by providing validation." />
    <siteMapNode url="~/EditInsertDelete/CustomizedUI.aspx"
        title="Customize the User Interface"
        description="Customize the editing and inserting user interfaces." />
    <siteMapNode url="~/EditInsertDelete/OptimisticConcurrency.aspx"
        title="Optimistic Concurrency"
        description="Learn how to help prevent simultaneous users from
                      overwritting one another&apos;s changes." />
    <siteMapNode url="~/EditInsertDelete/ConfirmationOnDelete.aspx"
        title="Confirm On Delete"
        description="Prompt a user for confirmation when deleting a record." />
    <siteMapNode url="~/EditInsertDelete/UserLevelAccess.aspx"
        title="Limit Capabilities Based on User"
        description="Learn how to limit the data modification functionality
                      based on the user role or permissions." />
</siteMapNode>

更新Web.sitemap 后,花些时间用浏览器查看一下教程网站。现在,左侧的菜单包含了对应编辑、插入和删除教程的项目。

图4 :站点地图现在包含了对应编辑、插入和删除教程的项目

步骤2 :添加和配置ObjectDataSource 控件

由于GridView 、DetailsView 和FormView 的数据修改功能和布局各不相同,下面我们对它们进行逐个分析。但与其让每个控件使用自己的 ObjectDataSource ,不如创建一个 ObjectDataSource ,供这三个控件的示例共同使用。

打开Basics.aspx 页面,从 Toolbox 上将一个 ObjectDataSource 拖放 到 Designer 中,并从该控件的智能标记上单击 Configure Data Source 链接。由于 ProductsBLL 是唯一提供编辑、插入和删除方法的 BLL 类,因此应配置 ObjectDataSource 使用此类。

图5 :配置 ObjectDataSource 使用 ProductsBLL 类

在下一屏中,通过选择相应的选项卡并从下拉列表中选择方法,我们就 可以指定将 ProductsBLL 类的哪些方法映射到 ObjectDataSource 的 Select() 、Insert() 、Update() 和 Delete() 方法。现在,图 6 看起来应该比较熟悉了,它将 ObjectDataSource 的 Select() 方法映射到 ProductsBLL 类的 GetProducts() 方法。通过选择顶端列表中的相应选项卡,可以对 Insert() 、 Update() 和 Delete() 方法进行配置。

图6 :使该 ObjectDataSource 返回所有产品

图7 、8 、9 显示了 ObjectDataSource 的 UPDATE 、INSERT 和 DELETE 选项卡。配置这些选项卡,使 Insert() 、 Update() 和 Delete() 三种方法分别调用 ProductsBLL 类的 AddProduct 、 UpdateProduct 和 DeleteProduct 方法。

图7 :将 ObjectDataSource 的 Update() 方法映射到 ProductBLL 类的 UpdateProduct 方法

图8 :将 ObjectDataSource 的 Insert() 方法映射到 ProductBLL 类的 AddProduct 方法

图9 :将 ObjectDataSource 的 Delete() 方法映射到 ProductBLL 类的 DeleteProduct 方法

你可能已经注意到,UPDATE 、INSERT 和 DELETE 选项卡的下拉列表中已经选择了各自的方法。这是因为我们使用了 DataObjectMethodAttribute ,它修饰了 ProductsBLL 的方法。例如, DeleteProduct 方法有如下签名:

[System.ComponentModel.DataObjectMethodAttribute
    (System.ComponentModel.DataObjectMethodType.Delete, true)]
public bool DeleteProduct(int productID)
{
    ...
}

DataObjectMethodAttribute 属性指出了每个方法的作用( 用于查询、插入、更新或删除 )以及是否为默认值。如果在创建 BLL 类时忽略这些属性,则需要从 UPDATE 、 INSERT 及 DELETE 选项卡中手动选择方法。

确保将相应的ProductsBLL 方法映射到 ObjectDataSource 的Insert() 、Update() 和 Delete() 方法后,单击Finish 完成向导。

检查ObjectDataSource 的标记

通过 ObjectDataSource 的 向导对其进行配置后,转至Source 视图查看生成的声明性标记。<asp:ObjectDataSource> 标记指定了基础对象和要调用的方法。此外,还有 DeleteParameters 、UpdateParameters 和InsertParameters ,它们映射到 ProductsBLL 类的 AddProduct 、 UpdateProduct 和 DeleteProduct 方法的输入参数:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
    DeleteMethod="DeleteProduct" InsertMethod="AddProduct"
    OldValuesParameterFormatString="original_{0}" SelectMethod="GetProducts"
    TypeName="ProductsBLL" UpdateMethod="UpdateProduct">
    <DeleteParameters>
        <asp:Parameter Name="productID" Type="Int32" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="productName" Type="String" />
        <asp:Parameter Name="supplierID" Type="Int32" />
        <asp:Parameter Name="categoryID" Type="Int32" />
        <asp:Parameter Name="quantityPerUnit" Type="String" />
        <asp:Parameter Name="unitPrice" Type="Decimal" />
        <asp:Parameter Name="unitsInStock" Type="Int16" />
        <asp:Parameter Name="unitsOnOrder" Type="Int16" />
        <asp:Parameter Name="reorderLevel" Type="Int16" />
        <asp:Parameter Name="discontinued" Type="Boolean" />
        <asp:Parameter Name="productID" Type="Int32" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="productName" Type="String" />
        <asp:Parameter Name="supplierID" Type="Int32" />
        <asp:Parameter Name="categoryID" Type="Int32" />
        <asp:Parameter Name="quantityPerUnit" Type="String" />
        <asp:Parameter Name="unitPrice" Type="Decimal" />
        <asp:Parameter Name="unitsInStock" Type="Int16" />
        <asp:Parameter Name="unitsOnOrder" Type="Int16" />
        <asp:Parameter Name="reorderLevel" Type="Int16" />
        <asp:Parameter Name="discontinued" Type="Boolean" />
    </InsertParameters>
</asp:ObjectDataSource>

ObjectDataSource 包含的参数与 相关方法的每个输入参数对应,这就如同当ObjectDataSource 被配置为调用一个准备接受输入参数( 如 GetProductsByCategoryID(categoryID) )的查询方法时,所显示的SelectParameters 列表。稍后我们将看到,这些 DeleteParameters 、UpdateParameters 和 InsertParameters 的值由 GridView 、DetailsView 和 FormView 在调用 ObjectDataSource 的 Insert() 、Update() 或 Delete() 方法之前自动设置。根据需要,这些值也可以通过编码设置。这些内容将在后面的教程中介绍。

使用向导配置ObjectDataSource 的另一个影响是,Visual Studio 将OldValuesParameterFormatString属性 设为 original_{0} 。此属性值用于包含被编辑数据的初始值,在下列两种场景中非常有用 :

  • 用户在编辑记录时可以更改主键值。在这种情况下,必须同时提供新的主键值和初始主键值。这样,我们才能查找到带有初始主键值的记录,并作相应的更新。
  • 使用并发优化。并发优化技术用于确保两个同时操作的用户不会覆盖彼此的更改。后面的教程将对此作详细讲解。

OldValuesParameterFormatString 属性表示了基础对象的更新和删除方法中对应初始值的输入参数的名称。在讨论并发优化时,我将对此属性及其作用作详细介绍。此处提到此属性是因为,我们的 BLL 的方法不接受初始值,因此必须将它删除。如果 OldValuesParameterFormatString 属性没有设为默认值 {0} ,Web 数据控件试图调用 ObjectDataSource 的 Update() 或 Delete() 方法时会出现错误。因为 ObjectDataSource 将试图同时传递指定的 UpdateParameters (或 DeleteParameters )和初始值参数。

如果现在对此问题不是很清楚也没关系,我们将在 后面的教程中详细研究该属性及其用途。现在,只需确保从声明性语法中完全删除此属性的声明,或将它的值设为默认的 {0} 即可。

注意:如果只是从 Design 视图的 Properties 窗口中清除OldValuesParameterFormatString 属性值,该属性在声明性语法中仍然存在,但被设置为空字符串。遗憾的是,这也会导致出现上面提到的问题。因此,应从声明性语法中将此属性删除,或者从 Properties 窗口中将属性值设为默认的 {0} 。





上一页 | 1 | 2 | 3 | 下一页