简体   繁体   English

ASP.NET动态数据在单个页面上的多个关系实体

[英]Asp.net Dynamic data multiple relational entities on single page

How to create form out of multiple relational entities with Asp.net dynamic data. 如何使用Asp.net动态数据从多个关系实体中创建表单。

Eg I have a customer table associated with address master. 例如,我有一个与地址主表关联的客户表。 (1 -> 0.1) (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. 我如何通过动态数据支架实现这一目标。

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. 首先,您应该自定义Insert.aspxEdit.aspx页面模板如何:通过使用自定义页面模板自定义单个表的布局,以便在自定义页面上放置其他(GridView或FormView)控件,以显示另一个控件实体。

The next step is the following. 下一步是以下步骤。 Consider example of editing customer. 考虑编辑客户的示例。

~/DynamicData/CustomPages/Customer/Edit.aspx (partially): 〜/ DynamicData / CustomPages / Customer / 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 with Address entity - Version 1 with QueryExtender and DynamicRouteExpression 具有地址实体的GridView-具有QueryExtender和DynamicRouteExpression的版本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>

In Version 1 we use a special type of data source expression DynamicRouteExpression . 版本1中,我们使用一种特殊类型的数据源表达式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. 在运行时,此对象从URL中提取主键列的值,并修改AddressDataSource生成的查询以包括适当的过滤条件。

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. 必须注意,表(实体) 地址必须包括列名Customer_Id (而不是例如Address_Customer_Id ),否则GridViewAddress将包括所有地址。


GridView with Address entity - Version 2 with QueryExtender with CustomExpression and OnQuerying is more powerful version 具有地址实体的GridView-具有QueryExtender和CustomExpression和OnQuerying的版本2是更强大的版本

<%-- 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>

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. 为了实现版本2,我们应该首先使用EditCustomerDataSource的 Selected事件,以便从EntityDataSourceSelectedEventArgs获取Customer_Id ,该实体Selected事件提供数据,然后可以使用QueryExtender控件使用的CustomExpression 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 . 定制表达式调用AddressQueryExtender_Querying方法,在这里我们应该使用定制LINQ表达式,并且过滤操作的结果(来自EntityDataSourceSelectedEventArgsCustomer_Id )将显示在GridViewAddress中

Code-behind: 后台代码:

~/DynamicData/CustomPages/Customer/Edit.aspx.cs (partially): 〜/ DynamicData / CustomPages / Customer / 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);
}

More detailed information and explanations you can find from the book ASP.NET Dynamic Data Unleashed . 您可以从《 ASP.NET动态数据释放 》一书中找到更多详细信息和说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM