数据使用EntityDataSource过滤器 [英] Filter Data using EntityDataSource

查看:462
本文介绍了数据使用EntityDataSource过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用EF 4,C#和MS成员资格提供。

I use EF 4, C# and MS Membership Provider.

我有一个数据源的GridView的EntityDataSource Web控件。

I have a GridView with DataSource an EntityDataSource web control.

我想使用EntityDataSource想过滤数据,过滤显示适用于当前登录的用户,此值应使用MS Memebership提供商采取(Membership.GetUser())。

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(); ).

现在,我不能在INF任何EntityDataSource参数,让我给点了(在使用提供的参数/自动生成凡恩pression)。

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

你有什么想法?

请为我提供code的样本。感谢您的时间!

Please provide me a sample of code. Thanks for your time!

推荐答案

您不能在声明标记直接绑定用户的身份到EntityDataSource。但基本上有两种解决方法:

You cannot bind the user's identity declaratively in markup directly to the EntityDataSource. But there are basically two workarounds:

首先是容易之一,但需要C-背后$ C $。您可以使用一个通用的 ASP:参数和code-背后将其值设置为用户的身份:

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: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-背后:

Code-behind:

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

第二种方法是创建一个自定义参数即可。一个例子是如何做到这一点是这里显示。注意:这个例子是基于 ASP:SqlDataSource的,但如果你确保你使用它应该工作以及为EntityDataSource WhereParameters 在EntityDataSource而不是 SelectParameters 在SqlDataSource的。

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.

这篇关于数据使用EntityDataSource过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆