Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

ASP.NET Load WebUserControl Programmatically and Invoke Method using Reflection

0.00/5 (No votes)
10 Jan 2011CPOL 6.1K  
Why invoke methods on a user web control that is required to be loaded dynamically
In this post, I list three things to note about why you would want to invoke methods on a user web control that is required to be loaded dynamically.

Perhaps you should explain a bit regarding the context of this tip.
e.g. Why would you want to invoke methods on a user web control that is required to be loaded dynamically?

There are three things:

  1. It sounds to me as a code smell: apparently, the control contains logic that is used by other methods outside of the control. Therefore, a better solution would be to encapsulate that logic in a reusable component outside of the webcontrol, so the webcontrol would not need to be loaded dynamically in the first place.
  2. Most of the example code demonstrates the use of reflection, which is not specific to web controls and therefore, kind of miss the point of this tip/trick.
  3. In my opinion also, the use of reflection is a code smell. Dynamic execution of methods (without compile-time type checking) should be avoided whenever possible as it may introduce runtime errors and reduces maintainability: code becomes less readable and understandable.

History

  • 12th March, 2010: Initial version

====================================
just because you can, doesn't mean you should.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)