2011-01-12 35 views
6

Xin chào Tôi có một EntityDataSource.Lọc dữ liệu bằng EntityDataSource và WHERE

Tôi cần lập trình GỬI một biến (@SelectedValue) để được sử dụng trong bộ lọc WHERE cho EntityDataSource.

Bạn có thể đăng lõi đơn giản để chỉ cho tôi cách thực hiện không? Cảm ơn vì đã dành thời gian cho tôi!

Để tạo WhereParameters trên EntityDataSource tôi sử dụng mã này:

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

Ở đây mã cho điều khiển:

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

Trả lời

6

đọc này?

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


Cập nhật: Một ví dụ với Northwind Products và Categories Table.
DropDownList liệt kê Danh mục và GridView hiển thị Sản phẩm được lọc theo Danh mục.

Các 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> 

Các 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); 
} 

Đây có phải là những gì bạn đang tìm kiếm?

+0

Cảm ơn tôi đọc nó, tôi vẫn cần phải biết làm thế nào để pr ogrammatic GỬI một giá trị cho EntityDataSet, bất kỳ ý tưởng? cảm ơn – GibboK

+0

Liên kết và giải thích tuyệt vời! +1 chắc chắn! – H27studio

+0

Liên kết đó hiện đã bị hỏng, nhưng điều này có thể là: [link] (http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet -getting-started-part-3) – PeterX

4

Tôi đã luôn luôn thay đổi DefaultValue để thiết lập một tham số từ mã phía sau, như vậy:

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

Nó làm việc cho tôi.

Edit: Sau đó bạn có thể xác định WhereParameter trong aspx-File và không cần phải thêm nó vào bộ sưu tập WhereParameters trong 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> 
0

tôi sử dụng này trên các trang của tôi để điền vào một lưới

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

lưới gọi entitydatasource của thành viên này

DataSourceID="edsOperacionData" 
Các vấn đề liên quan