单页上Asp.net动态数据的多个实体关系 [英] Asp.net Dynamic data multiple relational entities on single page

查看:288
本文介绍了单页上Asp.net动态数据的多个实体关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建表单用了动态Asp.net数据的多个关系的实体。

How to create form out of multiple relational entities with Asp.net dynamic data.

例如。我有一个主地址相关联的客户表。 (1 - > 0.1)

E.g. I have a customer table associated with address master. (1 -> 0.1)

我想说明这两个实体到一个单一页面,同时创建和编辑的客户。

I want to show both these entities to a single page while create and editing customer.

我怎么能与动态数据脚手架实现这一目标。

How could I achieve this with dynamic data scaffolding.

推荐答案

首先,你应该定义的 Insert.aspx 的和的 Edit.aspx 的页面模板的如何:自定义单个表的布局使用自定义页面模板为了把自定义页面的附加(GridView控件或FormView控件),用于显示另一个实体的控制上。

First of all, you should customize Insert.aspx and Edit.aspx page templates How to: Customize the Layout of an Individual Table By Using a Custom Page Template in order to place on the custom page additional (GridView or FormView) control for showing another entity.

下一步是以下内容。考虑例编辑的客户。

The next step is the following. Consider example of editing customer.

〜/ DynamicData / CustomPages /客户/ Edit.aspx (部分):

<%-- FormView with Customer entity --%>

<asp:FormView 
    ID="FormViewEditCustomer" 
    runat="server" 
    DataSourceID="EditCustomerDataSource" 
    DefaultMode="Edit"
    OnItemCommand="FormViewEditCustomer_ItemCommand"
    OnItemDeleted="FormViewEditCustomer_ItemDeleted" 
    RenderOuterTable="false">
    <EditItemTemplate>
        <table id="editTable" class="table-edit" cellpadding="6">
            <asp:DynamicEntity runat="server" Mode="Edit" />
        </table>
    </EditItemTemplate>
</asp:FormView>
<asp:EntityDataSource 
    ID="EditCustomerDataSource" 
    runat="server" 
    EnableUpdate="true" 
    EnableDelete="true" 
    OnUpdated="EditCustomerDataSource_Updated"
    OnSelected="EditCustomerDataSource_Selected"/>
<asp:QueryExtender 
    ID="EditCustomerQueryExtender"
    TargetControlID="EditCustomerDataSource" 
    runat="server">
    <asp:DynamicRouteExpression />
</asp:QueryExtender>

GridView控件地址的实体 - 用的QueryExtender和DynamicRouteEx pression版本1

<%-- GridView with Address entity - Version 1 with DynamicRouteExpression --%>

<asp:GridView 
    ID="GridViewAddress" 
    runat="server" 
    DataSourceID="AddressDataSource"
    AllowPaging="true" 
    AllowSorting="false" 
    PageSize="10" 
    CssClass="table-list" 
    AutoGenerateColumns="true">
</asp:GridView>
<asp:EntityDataSource 
    ID="AddressDataSource" 
    runat="server"
    ConnectionString="name=Entities" 
    DefaultContainerName="Entities"
    EntitySetName="Address" />
<asp:QueryExtender 
    ID="AddressQueryExtender" 
    TargetControlID="AddressDataSource"
    runat="server">
    <asp:DynamicRouteExpression ColumnName="Customer_Id" />
</asp:QueryExtender>

版本1 :我们使用一种特殊类型的数据源的前pression的 DynamicRouteEx pression 。在运行时,该对象的网址提取主键列的值,并修改所产生的查询在 AddressDataSource 以包括适当的筛选条件。

In Version 1 we use a special type of data source expression DynamicRouteExpression. At runtime, this object extracts the values of the primary key columns from the URL and modifies the query generated by the AddressDataSource to include appropriate filter criteria.

但必须指出的是,表(实体)的地址必须包含列名的 CUSTOMER_ID (和不可以例如 Address_Customer_Id ),否则 GridViewAddress 将包含所有的地址。

It must be noted that the table (entity) Address must include column name Customer_Id(and NOT for example Address_Customer_Id), otherwise GridViewAddress will include all addresses.

GridView控件地址的实体 - 用的QueryExtender与CustomEx pression和OnQuerying版本2 是更强大的版本。

GridView with Address entity - Version 2 with QueryExtender with CustomExpression and OnQuerying is more powerful version

<%-- GridView with Address entity - Version 2 with QueryExtender with CustomExpression and OnQuerying --%>

<asp:GridView 
    ID="GridViewAddress" 
    runat="server" 
    DataSourceID="AddressDataSource"
    AllowPaging="true" 
    AllowSorting="false" 
    PageSize="10" 
    CssClass="table-list" 
    AutoGenerateColumns="true">
</asp:GridView>
<asp:EntityDataSource 
    ID="AddressDataSource" 
    runat="server"
    ConnectionString="name=Entities" 
    DefaultContainerName="Entities"
    EntitySetName="Address" />
<asp:QueryExtender 
    ID="AddressQueryExtender" 
    TargetControlID="AddressDataSource"
    runat="server">
    <asp:CustomExpression 
        OnQuerying="AddressQueryExtender_Querying" />
</asp:QueryExtender>

为了落实版本2 我们应该使用,首先,选择的事件的 EditCustomerDataSource ,以获得 CUSTOMER_ID 来自的 EntityDataSourceSelectedEventArgs 的,这为选择的事件,那么我们就可以使用 CustomEx pression 所使用的提供数据的QueryExtender 控制。自定义前pression调用的 AddressQueryExtender_Querying 方式,在这里我们应该用一个自定义的LINQ前pression和过滤操作的结果(由 CUSTOMER_ID EntityDataSourceSelectedEventArgs 的)将显示在一个 GridViewAddress

In order to implement Version 2 we should use, first of all, Selected event of EditCustomerDataSource in order to get Customer_Id from EntityDataSourceSelectedEventArgs, that provides data for the Selected event, then we can use CustomExpression that is used by the QueryExtender control. The custom expression calls the AddressQueryExtender_Querying method, where we should use a custom LINQ expression and the results of the filtering operation (by Customer_Id from EntityDataSourceSelectedEventArgs) will be displayed in a GridViewAddress.

code-背后:

〜/ DynamicData / CustomPages /客户/ Edit.aspx.cs (部分):

protected int customerId;

protected void EditCustomerDataSource_Selected(object sender, EntityDataSourceSelectedEventArgs e)
{
    IEnumerable<Customer> customerItem = e.Results.Cast<Customer>();
    foreach (Customer c in customerItem)
    {
        customerId = c.Customer_Id;
    }
}

protected void AddressQueryExtender_Querying(object sender, System.Web.UI.WebControls.Expressions.CustomExpressionEventArgs e)
{
    e.Query = (from a in e.Query.Cast<Address>()
               where a.Customer_Id == customerId
               select a);
}

更详细的信息,并解释你可以从书中找到 ASP.NET动态数据偷跑

这篇关于单页上Asp.net动态数据的多个实体关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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