使用EntityDataSource和WHERE过滤数据

时间:2011-01-12 11:31:53

标签: c# asp.net frame entitydatasource

您好我有一个EntityDataSource。

我需要以编程方式发送一个变量(@SelectedValue),以便在EntityDataSource的WHERE过滤器中使用。

你能发一个简单的核心来告诉我该怎么做吗?谢谢你的时间!

要在EntityDataSource上创建WhereParameters,我使用以下代码:

            Parameter parameter = new Parameter("SelectedValue", TypeCode.Int32, uxTreeView1.SelectedValue);
            parameter.DefaultValue = "0";
            uxEntityDataSourceNodes.WhereParameters.Add(parameter);`

这里是Control的代码:

        <asp:EntityDataSource ID="uxEntityDataSourceNodes" runat="server" 
        ConnectionString="name=TestHierarchyEntities" 
        DefaultContainerName="TestHierarchyEntities" EnableFlattening="False" 
        EnableUpdate="True" EntitySetName="CmsCategories" Where="it.CategoryId = @SelectedValue" 
        EntityTypeFilter="" Select="">
    </asp:EntityDataSource>

3 个答案:

答案 0 :(得分:7)

读这个?

The Entity Framework and ASP.NET - Filtering, Ordering, and Grouping Data

<小时/> 更新:Northwind产品和类别表的示例 DropDownList列出类别,GridView显示按类别筛选的产品。

ASPX

<asp:DropDownList ID="uxTreeView1" runat="server" 
            AutoPostBack="true"
            AppendDataBoundItems="true"
            DataSourceID="EntityDataSource1" 
            DataTextField="CategoryName" 
            DataValueField="CategoryID" 
            OnSelectedIndexChanged="uxTreeView1_SelectedIndexChanged">
    <asp:ListItem Text="Select Category" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=NorthwindEntities" 
    DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
    EntitySetName="Categories" Select="it.[CategoryID], it.[CategoryName]">
</asp:EntityDataSource>
<asp:GridView ID="GridView1" runat="server" 
            AutoGenerateColumns="False" 
            DataSourceID="EntityDataSource2"
            DataKeyNames="ProductID">
    <Columns>
        <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
            ReadOnly="True" SortExpression="ProductName" />
        <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" 
            ReadOnly="True" SortExpression="CategoryID" />
    </Columns>
</asp:GridView>
<asp:EntityDataSource ID="EntityDataSource2" runat="server" 
    ConnectionString="name=NorthwindEntities" 
    DefaultContainerName="NorthwindEntities" EnableFlattening="False" 
    EntitySetName="Products" 
    Select="it.[ProductID], it.[ProductName], it.[CategoryID]">
</asp:EntityDataSource>

ASPX.CS

protected void uxTreeView1_SelectedIndexChanged(object sender, EventArgs e)
{
    EntityDataSource2.WhereParameters.Clear();
    EntityDataSource2.AutoGenerateWhereClause = true;
    //alternatively
    //EntityDataSource2.Where = "it.[CategoryID] = @CategoryID";
    EntityDataSource2.WhereParameters.Add("CategoryID", TypeCode.Int32, uxTreeView1.SelectedValue);
}

这是你在找什么?

答案 1 :(得分:4)

我总是更改DefaultValue来设置代码隐藏的参数,如下所示:

uxEntityDataSourceNodes.WhereParameters["SelectedValue"].DefaultValue
    = uxTreeView1.SelectedValue.ToString();

它对我有用。

编辑:然后您可以在aspx-File中指定WhereParameter,而不需要将其添加到代码隐藏中的WhereParameters集合中:

<asp:EntityDataSource ID="uxEntityDataSourceNodes" runat="server" 
    ConnectionString="name=TestHierarchyEntities" 
    DefaultContainerName="TestHierarchyEntities" EnableFlattening="False" 
    EnableUpdate="True" EntitySetName="CmsCategories"
    Where="it.CategoryId = @SelectedValue" 
    EntityTypeFilter="" Select="">
    <WhereParameters>
        <asp:Parameter Name="SelectedValue" Type="Int32" />
    </WhereParameters>
</asp:EntityDataSource>

答案 2 :(得分:0)

我在我的页面上使用它来填充网格

 <ef:EntityDataSource runat="server" ID="edsOperacionData"
     ConnectionString="name=VistasInntecMPContext"
     DefaultContainerName="VistasInntecMPContext"
     EnableFlattening="False"
     EntitySetName="OperacionDatas"
     OrderBy="it.[ClaveEmpleado]"
     Select="it.[TarjetaID], it.[ClienteID], it.[ClienteID2], it.[ClaveEmpleado], it.[ProductoGrupoID], it.[ProductoNombre], it.[NoTarjeta], it.[TipoTarjeta], it.[StatusCuentaID], it.[StatusTarjetaID], it.[StatusTarjeta], it.[TarjetaHabienteID]"
     Where="(it.[ProductoGrupoID] = @ProductoGrupoID OR @ProductoGrupoID is null) AND (it.[ClienteID2] = @ClienteId2 OR @ClienteId2 is null) AND (it.[NoTarjeta] = @NoTarjeta OR @NoTarjeta is null) AND (@Clave is null OR it.[ClaveEmpleado] = @Clave) AND (@Estatus is null OR it.[StatusTarjetaID] = @Estatus)">                            
     <WhereParameters>
         <asp:ControlParameter ControlID="drpProductosB" DbType="Byte"
             Name="ProductoGrupoID" PropertyName="SelectedValue" />
         <asp:ControlParameter ControlID="txtClienteIdB" DbType="String"
             Name="ClienteId2" PropertyName="Text" />
         <asp:ControlParameter ControlID="txtClaveEmpleadoB" DbType="String"
             Name="Clave" PropertyName="Text" />
         <asp:ControlParameter ControlID="txtNoTarjetaB" DbType="String"
             Name="NoTarjeta" PropertyName="Text" DefaultValue="" />
         <asp:ControlParameter ControlID="drpEstatusB" DbType="Byte"
             Name="Estatus" PropertyName="SelectedValue" />
     </WhereParameters>
 </ef:EntityDataSource>

网格通过此

调用entitydatasource
DataSourceID="edsOperacionData"