Filter Data using EntityDataSource

前端 未结 1 1079
青春惊慌失措
青春惊慌失措 2021-01-13 18: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

相关标签:
1条回答
  • 2021-01-13 19:35

    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.

    0 讨论(0)
提交回复
热议问题