ASP.NET Property Parameter for Data Sources

A few years ago I wrote a custom parameter for data sources such as the ObjectDataSource which used the value of a property on the page for a parameter.

I needed this functionality again so I found what I had previously written and improved it to make it more usable.


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

public class PropertyParameter : Parameter
{
    public string PropertyName { get; set; }

    protected override object Evaluate(HttpContext context, Control control)
    {
        Control propertyControl = GetContainingControl(control);
        if (propertyControl != null)
        {
            return DataBinder.Eval(propertyControl, PropertyName);
        }
        else
        {
            throw new ApplicationException(string.Format("Unable to find property: {0}", PropertyName));
        }
    }

    private Control GetContainingControl(Control control)
    {
        //Check if the property exists in the control
        PropertyInfo propertyInfo = control.GetType().GetProperty(PropertyName);

        if (propertyInfo != null)
        {
            //Check that the property type matching the type set for this parameter
            if (System.Type.GetTypeCode(propertyInfo.PropertyType).Equals(Type))
            {
                return control;
            }
        }

        //Check if the control has a parent, and if so call this method again passing the parent as the control
        if (control.Parent == null)
        {
            return null;
        }
        else
        {
            return GetContainingControl(control.Parent);
        }  �
    }
}

The parameter has a custom property called PropertyName which is the name of the property to get the value from.

The method GetContainingControl takes the control passed by the overridden Evaluate method to ensure that the container of the data source has a property matching the provided name.  As the data source may be in a panel or user control, it recursively navigates up the control hierarchy and using reflection checks if the property exists on the current control.  If so it checks that the property’s type matches that of the Type property set on the parameter.

The overridden Evaluate method uses the GetContainingControl method to get the control containing the property, and then uses DataBinder.Eval to evaluate the value of the property and return it.

To use the parameter create a reference to the namespace and assembly if necessary.

<%@ Register TagPrefix="controls" Namespace="Controls" Assembly="Controls" %>

I may then have a property on my page or user control called UserID which I want to use for a parameter value:

public int UserID { get; set; }

I could then use the parameter inside one of the data source’s parameter collections, for example the SelectParameter collection:

<SelectParameters>
    <controls:PropertyParameter PropertyName="UserID" Name="UserID" Type="Int32"></controls:PropertyParameter>
</SelectParameters>
Posted on by Joe in ASP.NET, C#

3 Responses to ASP.NET Property Parameter for Data Sources

  1. Daniel Ballinger

    The PropertyParameter works great.

    I’ll use some of your ads to say thanks.

  2. Joe

    Thanks Daniel

    Glad you found it useful

    Joe

  3. Ekhard

    Thanks Joe for this blog post, but I have a question regarding the following:

    When I set the PropertyParameter in the codebehind the PropertyParameter will not find my specified Property.

    ObjectDataSource myDataSource = new ObjectDataSource();

    myDataSource.SelectParameters.Add(new PropertyParameter() { PropertyName = “myProperty”, Name = “myproperty”, Type = TypeCode.Object })

    In the function “Evaluate(HttpContext context, Control control)”, the control value is nearly empty. I can’t find the reference to the propertyName.

    any suggestions?

Add a Comment