2011-01-12 2 views
6

こんにちは私はEntityDataSourceを持っています。EntityDataSourceとWHEREを使用したデータのフィルタリング

EntityDataSourceのWHEREフィルタで使用する変数(@SelectedValue)をプログラムで送信する必要があります。

シンプルなコアを投稿して、その方法を教えてもらえますか?御時間ありがとうございます!コントロールのためにここに

  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> 

答えて

6

はこれを読んで

は、私はこのコードを使用EntityDataSourceにWhereParametersを作成するには?

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

が、これはあなたが探しているものですか?

+0

おかげでentitydatasourceを呼び出して、私はまだ知っている必要がありますどのようにPRへogrammatic EntityDataSetに値を送るかどうか?ありがとうございました – GibboK

+0

すばらしいリンクと説明! +1してください! – H27studio

+0

そのリンクは現在壊れていますが、これはそれかもしれません:[link](http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet -getting-started-part-3) – PeterX

4

私はいつものように、コードビハインドからパラメータを設定する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> 
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" 
関連する問題