使用 EntityDataSource 和 WHERE 过滤数据

发布于 2024-10-11 14:28:08 字数 881 浏览 1 评论 0原文

嗨,我有一个 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>

Hi I have an EntityDataSource.

I need programmatically SEND a variable (@SelectedValue) 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

游魂 2024-10-18 14:28:09

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

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

它对我有用。

编辑:然后您可以在 aspx 文件中指定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>

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>
被你宠の有点坏 2024-10-18 14:28:09

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

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

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"
时间海 2024-10-18 14:28:08

读这个?

实体框架和 ASP.NET - 过滤、排序和分组数据


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

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

这是您要找的吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文