Creating Data Source Control by Inheriting From ObjectDataSource
Data binding features of ASP.NET is awesome; the only pitfall with this technique is that most of the code tends to be “declarative” and you loose compile time checks which increases chances of error in maintenance. However using ObjectDataSource you can resolve some of the “page maintenance” issues resolved but it increases the code complexity.
Earlier I mentioned the approach we should opt; the theory is simple; move as many things to code behind and class libraries as possible so that you get your compile time error checking back. (Compilers are great to report breaking changes). Recently after studying ASP.NET 2.0 MVP Hacks and Tips book; I have started recommending writing your own object data source inherited data source control (preferably in UI layer projects only; keep your business layer projects free from System.Web assembly dependency).
Today I happened to fix an issue with a deployed application; and I thought that it’s a great opportunity for me to use the technique myself that I have been telling others. Here are few screenshots of my today’s work.
A simple Select-Only data-source
With this; we can use <SelectParameters /> section in ASCX/ASPX declaration of the control. But I preferred to write few more lines for more maintainable thing.
Now to use this; all we need is to specify the SelectControlID in declaration; something like:
Preferably; we should set this property in the code behind:
Here is the final declaration of the datasource in ASCX/ASPX