如何在EntityDataSource中为WhereParameters设置参数

时间:2010-12-29 08:32:42

标签: c# asp.net

我有一个EntityDataSource,我需要将WHERE设置为局部变量类型:GUID。

我的问题是我无法将我的本地变量Guid发送到EntityDataSource以进行WHERE操作。

我还尝试使用ControlParameter <asp:ControlParameter Name="UserId" />并在我的Guid上使用Text属性转换为String。但是不起作用。

任何想法如何解决问题

   <asp:EntityDataSource ID="EntityDataSourceListAuthors" runat="server" 
        AutoGenerateWhereClause="True" 
        ConnectionString="name=CmsConnectionStringEntityDataModel" 
        DefaultContainerName="CmsConnectionStringEntityDataModel" 
        EnableFlattening="False" EntitySetName="CmsAuthors" Where="" 
        EntityTypeFilter="" Select="">
        <WhereParameters>
            <asp:Parameter Name="UserId" />
        </WhereParameters>
    </asp:EntityDataSource>

1 个答案:

答案 0 :(得分:5)

解决我的问题:

  • 添加Object
  • 类型的CUSTOM PARAMETER

有用的资源:

http://www.leftslipper.com/ShowFaq.aspx?FaqId=11

http://weblogs.asp.net/scottgu/archive/2006/01/23/436276.aspx

How to programmatically set parameters for EntityDataSource and DetailsView?

http://msdn.microsoft.com/en-us/library/cc294876%28v=Expression.40%29.aspx

http://msdn.microsoft.com/en-us/library/cc295043%28v=Expression.40%29.aspx

http://weblogs.asp.net/scottgu/archive/2006/11/26/tip-trick-how-to-register-user-controls-and-custom-controls-in-web-config.aspx

我的代码现在

<asp:EntityDataSource ID="EntityDataSourceListAuthors" runat="server" 
        AutoGenerateWhereClause="True" 
        ConnectionString="name=CmsConnectionStringEntityDataModel" 
        DefaultContainerName="CmsConnectionStringEntityDataModel" 
        EnableFlattening="False" EntitySetName="CmsAuthors" Where="" 
        EntityTypeFilter="" Select="">
        <WhereParameters>
            <cmsParameter:CustomParameter Name="UserId" />
        </WhereParameters>
    </asp:EntityDataSource>

新课程已添加:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Security;

namespace WebProject.Core.Utilities
{
    public class CustomParameter : Parameter
    {
        protected override object Evaluate(HttpContext context, Control control)
        {
            MembershipUser currentUser = Membership.GetUser();
            return currentUser.ProviderUserKey;
        }
    }
}