嗨,我有一个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>
最佳答案
读这个?
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);
}
这是你想要的?
关于c# - 使用EntityDataSource和WHERE过滤数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4668270/