简体   繁体   English

使用EntityDataSource过滤数据

[英]Filter Data using EntityDataSource

I use EF 4, C# and MS Membership Provider. 我使用EF 4,C#和MS会员提供商。

I have a GridView with DataSource an EntityDataSource web control. 我有一个带有DataSource的GridView和一个EntityDataSource Web控件。

I would like filter Data using EntityDataSource, filter show apply for the Current Logged-In User, this value should be taken using MS Memebership Provider ( Membership.GetUser(); ). 我想使用EntityDataSource过滤数据,过滤器显示申请当前登录用户,此值应使用MS Memebership Provider(Membership.GetUser();)。

Now I cannot inf any Parameter in EntityDataSource that would allow me to dot that (in Where/Automatically generate a Where expression using provided parameter ). 现在我不能在EntityDataSource中输入任何允许我点的那个参数(在Where /使用提供的参数自动生成Where表达式中)。

Do you have any ideas? 你有什么想法?

Please provide me a sample of code. 请提供一些代码示例。 Thanks for your time! 谢谢你的时间!

You cannot bind the user's identity declaratively in markup directly to the EntityDataSource. 您无法在标记中以声明方式将用户的标识直接绑定到EntityDataSource。 But there are basically two workarounds: 但基本上有两种解决方法:

The first is the easier one but requires code-behind. 第一个是更容易的但需要代码隐藏。 You can use a generic asp:Parameter and set its value in code-behind to the user's identity: 您可以使用通用的asp:Parameter并在代码隐藏中将其值设置为用户的标识:

Markup: 标记:

<asp:EntityDataSource ID="MyEntityDataSource" runat="server" 
    ConnectionString="name=MyContext" 
    DefaultContainerName="MyContext" 
    EntitySetName="MyObjectSet" 
    AutoGenerateWhereClause="False"
    Where="it.Username = @Username" 
    <WhereParameters>
        <asp:Parameter Name="Username" Type="String" />
    </WhereParameters>
</asp:EntityDataSource>

Code-behind: 代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    MyEntityDataSource.WhereParameters["Username"].DefaultValue =
        User.Identity.Name;
}

The second way is to create a custom parameter . 第二种方法是创建自定义参数 An example how to do that is shown here . 此处显示如何执行此操作的示例。 Note: The example is based on a asp:SqlDataSource but it should work as well for an EntityDataSource if you make sure that you use WhereParameters in the EntityDataSource instead of SelectParameters in the SqlDataSource. 注意:该示例基于asp:SqlDataSource但如果确保在EntityDataSource中使用WhereParameters而不是在SqlDataSource中SelectParameters ,则它也WhereParameters于EntityDataSource。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM