2013-01-13 One thing that's been a nagging concern since I launched halfgk.com six years ago was the no-man's land between the partial classes in the codebehind of web forms and the inclusion of code from a separate vb class. Over time I found myself wishing for a way to more seamlessly integrate the two -- but it seemed like anything dealing with a Page object just simply couldn't live in that class, and that forced me to leave more code in the Web forms -- code that was being repeated form after form after form. Thankfully, it doesn't have to be like this. My solution was to create a new public class which derives from System.Web.UI.Page, the same class from which the code-behind partial classes derive. Public Class CommonPage Inherits Page . . . End Class Next -- and this was the big PITA -- modify every web form to inherit from the new public class instead of directly from System.Web.UI.Page: Partial Class MyWebPage Inherits CommonPage . . . End Class ... and now you can port the common functions you have included on every page up to the CommonPage class. In my case, I was able to port an event handler and several private routines that are used on every web form. In doing this, you must change keyword usage from Private to Protected or Public, because if the routines are kept at Private inside of your public class, your web forms won't be able to access them. Using the Protected keyword ensures that the routine is accessible from derived classes -- which your web forms now are. Note that not every routine you move will require this change. Let's say you move a subroutine called GetData() into your new class. Let's say GetData() calls a function called LoadDataSet() which is used populate _dsDataSet which has been declared at the module level. Before the advent of the new public class, both GetData() and LoadDataSet() were Private routines that were called from every Web Form. But thanks to the magic of inheritance, you can move all of this goop up to your new Public class -- BUT not all of the keywords have to change. You can declare your dataset variable as Protected and your GetData() sub as Protected, but because LoadDataSet() is called from within the new Public class, there's no reason not to keep the keyword as Private -- because it's being called privately within the same class -- that is, there's no reason to expose LoadDataSet() to the derived Web Forms because it's not called from the Web Forms. You may find you'll have to modify some of your Protected routines to accept some parameters from the derived partial classes. Sticking with our examples of GetData() and LoadDataSet(), you may find that LoadDataSet uses a path value that must change depending on the Web Form that calls it. Simply change the routines to include a new parameter, and let your Web Forms send that parameter when calling GetData(): Protected _dsDataSet As DataSet . . . Protected Sub GetData(ByVal strPath As String) _dsDataSet = LoadDataSet(strPath) . . . End Sub Private Function LoadDataSet(ByVal strPath As String) As DataSet Dim dsDataSet As New DataSet Dim rdrReader As New XmlTextReader(Server.MapPath(strPath)) . . . Return dsDataSet End Function Once these changes are in place, your Web Forms will function as normal, because your Web Forms have access to the routines in the public class -- which is now a parent to each Web Form. When I made these changes, I cut at least 75% off of the size of each Web Form because the common code was now in that parent. Each Web Form now declares only a few variables and has only a couple private routines which remain. Everything else is in the parent class -- including several variables. I set the values for those variables (remember the path example above?) in the Page_Load() event handler in each Web Form. That should be it for now! Contact me using the site's contact form if you have questions. Feel free to use the code in your projects. A shout out in your project would be thoughtful. Also, drop me a line and let me know how you might have tweaked things to better suit your needs. Finally, I wouldn't profess to be THE expert on matters represented in my code -- so drop me a line if you have constructive suggestions, too. I'd like to hear from you! Best, halfgk copyright 2013 halfgk.com