Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6815921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:51:51+00:00 2026-05-26T20:51:51+00:00

I have a custom control where I want to expose a method as a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:51:52+00:00Added an answer on May 26, 2026 at 8:51 pm

    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:

    <uc1:MatrixTable ID="ucMatrixTable" runat="server" OnValidatingMatrix="ValidateMatrix" />
    

    Also, let’s use the CancelEventHandler delegate instead of Func<bool>. This means that your ValidateMatrix method signature in your code-behind would have to look like this:

    protected void ValidateMatrix(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // perform validation logic
    
        if (validationFailed)
        {
            e.Cancel = true;
        }
    }
    

    Inside of your MatrixTable custom control, implement something like this:

        const string ValidatingMatrixEventKey = "ValidatingMatrix";
    
        public event System.ComponentModel.CancelEventHandler ValidatingMatrix
        {
            add { this.Events.AddHandler(ValidatingMatrixEventKey, value); }
            remove { this.Events.RemoveHandler(ValidatingMatrixEventKey, value); }
        }
    
        protected bool OnValidatingMatrix()
        {
            var handler = this.Events[ValidatingMatrixEventKey] as System.ComponentModel.CancelEventHandler;
            if (handler != null)
            {
                // prepare event args
                var e = new System.ComponentModel.CancelEventArgs(false);
    
                // call the event handlers (an event can have multiple event handlers)
                handler(this, e);
    
                // if any event handler changed the Cancel property to true, then validation failed (return false)
                return !e.Cancel;
            }
    
            // there were no event handlers, so validation passes by default (return true)
            return true;
        }
    
        private void MyLogic()
        {
            if (this.OnValidatingMatrix())
            {
                // validation passed
            }
            else
            {
                // validation failed
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control that exposes a property. When I set it using
I have a custom DataGridView control and want to set the text format for
I have a custom control and I want to dynamically insert a link to
I have a custom control (C#, visual studio). I want to show a tooltip
I have a custom control: it's managed code, which subclasses System.Windows.Forms.Control. I want to
I want to create a custom control in C#. But every time I have
I want to double buffer a custom control which contains buttons. I have tried
I have a custom control whose primary purpose is to draw data. I want
I have a custom control with that test-code. Then I put my control on
Hello I have a custom control. This custom control has it's DataContext set to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.