2011-03-28 92 views
4

我使用EF 4,C#和MS成員資格提供程序。使用EntityDataSource篩選數據

我有一個GridView與DataSource的EntityDataSource Web控件。

我想過濾數據使用EntityDataSource,篩選器顯示申請當前登錄用戶,這個值應採取使用MS Memebership提供商(Membership.GetUser();)。

現在我不能inf入EntityDataSource中的任何參數,這將允許我點(在Where /自動生成一個Where表達式使用提供的參數)。

你有什麼想法嗎?

請給我一個代碼示例。謝謝你的時間!

回答

11

您不能將標記中的用戶標識直接綁定到EntityDataSource。但基本上有兩種解決方法:

第一種是比較容易的,但需要隱藏代碼。您可以使用一個通用的asp:Parameter和代碼隱藏其值設置爲用戶的身份:

標記:

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

代碼隱藏:

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

的第二種方法是創建一個自定義參數。如何做到這一點的例子顯示爲here。注意:該示例基於asp:SqlDataSource,但如果您確保在EntityDataSource中使用WhereParameters而不是在SqlDataSource中使用SelectParameters,它應該也適用於EntityDataSource。

+0

感謝Slauma,我遵循了您的建議,並實施了「自定義參數」。謝謝你的時間 – GibboK 2011-03-29 06:41:46