6

嗨,我有一个 EntityDataSource。

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

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

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

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

这里是控件的代码:

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

3 回答 3

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

这是你想要的?

于 2011-01-12T11:40:58.480 回答
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>
于 2011-01-12T16:16:11.277 回答
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>

网格由此调用实体数据源

DataSourceID="edsOperacionData"
于 2017-10-13T01:27:45.883 回答