Filter data using EntityDataSource

I am using EF 4, C # and MS Membership Provider.

I have a GridView with a DataSource EntityDataSource web element.

I would like to filter the data using EntityDataSource, show the filter in relation to the current user registered in the system, this value should be accepted using the MS Memebership Provider (Membership.GetUser ();).

Now I can’t insert any parameter in the EntityDataSource that would allow me to calculate this value (in Where / Automatically generate the Where expression using the provided parameter).

Do you have any ideas?

Please provide me a sample code. Thank you for your time!

+4
source share
1 answer

You cannot bind a user identifier declaratively in markup directly to an EntityDataSource. But there are basically two workarounds:

The first one is simpler, but requires code. You can use the generic asp:Parameter and set its value in the code for the user ID:

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 for:

 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 of how to do this is shown here . Note. The example is based on asp:SqlDataSource , but it should work for EntityDataSource if you make sure that you use WhereParameters in EntityDataSource instead of SelectParameters in SqlDataSource.

+11
source

Source: https://habr.com/ru/post/1345511/


All Articles