Filter Data using EntityDataSource

Deadly 提交于 2019-12-01 05:19:56

问题


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

I have a GridView with DataSource an EntityDataSource web control.

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

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

Do you have any ideas?

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


回答1:


You cannot bind the user's identity declaratively in markup directly to the 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:

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.



来源:https://stackoverflow.com/questions/5456150/filter-data-using-entitydatasource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!