嗨,我有一个EntityDataSource.
我需要以编程方式发送变量(@SelectedValue)以在EntityDataSource的WHERE过滤器中使用.
你可以发一个简单的核心来告诉我该怎么做吗?谢谢你的时间!
要在EntityDataSource上创建WhereParameters,我使用以下代码:
Parameter parameter = new Parameter("SelectedValue",TypeCode.Int32,uxTreeView1.SelectedValue); parameter.DefaultValue = "0"; uxEntityDataSourceNodes.WhereParameters.Add(parameter);`
这里是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>
解决方法
读这个?
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",uxTreeView1.SelectedValue); }
这是你想要的?