0

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

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

我想在创建和编辑客户时将这两个实体显示到一个页面。

我怎样才能通过动态数据脚手架来实现这一点。

4

1 回答 1

1

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

下一步如下。考虑编辑客户的例子。

~/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 -带有 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>

版本 1中,我们使用一种特殊类型的数据源表达式DynamicRouteExpression在运行时,此对象从URL中提取主键列的值,并修改由AddressDataSource生成的查询以包含适当的过滤条件。

必须注意,表(实体)地址必须包含列名Customer_Id(而不是例如Address_Customer_Id),否则GridViewAddress将包含所有地址。


带有地址实体的GridView -带有带有 CustomExpression 和 OnQuerying 的 QueryExtender 的版本 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>

为了实现版本 2,我们首先应该使用EditCustomerDataSource的Selected事件,以便从EntityDataSourceSelectedEventArgs获取Customer_Id ,它为Selected事件提供数据,然后我们可以使用 QueryExtender控件使用的CustomExpression 。自定义表达式调用AddressQueryExtender_Querying方法,我们应该在其中使用自定义 LINQ 表达式,过滤操作的结果(通过Customer_Id来自EntityDataSourceSelectedEventArgs)将显示在GridViewAddress.

代码隐藏:

~/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);
}

您可以从ASP.NET Dynamic Data Unleashed一书中找到更详细的信息和解释。

于 2013-04-08T15:47:35.247 回答