I have a custom control where I want to expose a method as a property ( e.g. for custom validation );
public Func<bool> ValidateMatrixFunc { get; set; }
then in the page that contains this custom control I can use a delegate or lambda exp to assign on OnPreInit event of the page;
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
ucMatrixTable.ValidateMatrixFunc = ValidateMatrix;
}
and this works.
However, I think it would be more convenient to do this in aspx, as in:
<uc1:MatrixTable ID="ucMatrixTable" runat="server" ValidateMatrixFunc="ValidateMatrix" />
But this crashes with the following message:
Cannot create an object of type ‘System.Func`1[[System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]’ from its string representation ‘ValidateMatrix’ for the ‘ValidateMatrixFunc’ property.
So, I just wonder… and i wonder.. if some ninja knows the answer to this, or it is just one of those mysteries of life we ll never get.
You might want to expose your “ValidateMatrixFunc” property as an event instead. Why? it’s more consistent with how controls are typically implemented. Also, events allow you to have multiple subscribers (event handlers) for a single event. While this might not be a typical use case, it does sometimes happen.
I’ve described below how I would implement this as an event:
Let’s call the event “ValidatingMatrix”.
Then you could write your ASPX markup like this:
Also, let’s use the
CancelEventHandlerdelegate instead ofFunc<bool>. This means that your ValidateMatrix method signature in your code-behind would have to look like this:Inside of your MatrixTable custom control, implement something like this: