开发者

Filtering data using EntityDataSource and WHERE

Hi I have an EntityDataSource.

I need programmatically SEND a variable (@SelectedV开发者_运维知识库alue) to be used in a WHERE Filter for the EntityDataSource .

Can you post a simple core to show me how to do it? Thanks for your time!

To create WhereParameters on EntityDataSource I use this code:

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

Here the code for the 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>


Read this?

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


Update: An example with Northwind Products and Categories Table.
DropDownList lists the Categories and the GridView displays the Products filtered by Category.

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

The 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);
}

Is this what you are looking for?


I have always changed the DefaultValue to set a parameter from code-behind, like so:

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

It worked for me.

Edit: You can then specify the WhereParameter in the aspx-File and don't need to add it to the WhereParameters collection in code-behind:

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


i use this on my pages to fill a grid

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

the grid calls the entitydatasource by this

DataSourceID="edsOperacionData"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜