I want to add a GridView
control to one of my views that is backed by a SqlDataSource
. The SelectCommand
query is parametrized on the aspnet_Users.UserId
GUID for the currently-logged-in user, so I need a way to pass the user ID as a parameter.
I read How to utilize ASP.NET current user name in SqlParameter without code-behind and decided to create a custom Parameter
named UserIdParameter
:
namespace MyApp.Web
{
public class UserIdParameter : Parameter
{
public UserIdParameter(string name)
: base(name)
{
}
protected UserIdParameter(UserIdParameter parameter)
: base(parameter)
{
}
protected override Parameter Clone()
{
return new UserIdParameter(this);
}
protected override object Evaluate(HttpContext context, Control control)
{
return Membership.GetUser().ProviderUserKey;
}
}
}
In the ASPX view I then added:
<%@ Register TagPrefix="my" Namespace="MyApp.Web" %>
in a line after the <%@ Page ... %>
line as well as:
<SelectParameters>
<my:UserIdParameter Name="UserId" />
</SelectParameters>
within the asp:SqlDataSource
element.
Unfortunately I get a Parser Error ("An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.") with the following highlighted in red:
<my:UserIdParameter Name="UserId" />
Visual Web Developer 2010 Express is also informing me that "Element 'UserIdParameter' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing."
Do I need to modify Web.config
in some way? If not, what do I need to do to be able to use my custom UserIdParameter
parameter?