views:

1466

answers:

2

Im stumped!

What is the best way to programmatically set a selecting parameter for the EntityDataSource control?

Specifically, I want to use the Page.User.ProviderUserKey to get a record in a custom User Details table I have, for a DetailsView.

I've seen code using the asp:ControlParameter to pull a value from a control, but that almost seems like a hack for my situation. I don't want an extra control just to set the parameter value.

Any ideas? Thanks in advance!

+1  A: 

You can create your own custom Parameter:

<my:CustomParameter Name="MyParam" />

The details of how to implement a custom parameter can be found on Fredrik Normén's Blog. Using a custom parameter is as simple as using any of the built-in parameter types, but a quick example can be found on ScottGu's Blog.

The simplest implementation would look something like this:

public class CustomParameter : Parameter
{
    protected override object Evaluate(HttpContext context, Control control)
    {
        // This is where you would grab and return
        // the Page.User.ProviderUserKey value
        return string.Empty;
    }
}
Brandon Gano
+1  A: 

If creating a custom parameter type feels like too much work, you can programatically add a basic parameter as follows:

Parameter parameter = new Parameter("MyParam", TypeCode.String,
    Page.User.ProviderUserKey);
MyDataSource.SelectParameters.Add(parameter);

Something like this should give you what you need.

Brandon Gano