Thursday, November 8, 2012

ISD Code Customization Tips, Part II: Reuse Code Snippet with Page Extension Method


Have you ever found yourself writing the same piece of code again and again on a highly costomized page? For example, on a page with a master record control and many detail table controls, you need to access a field in the master record from the detail tables. This is what the code snippet may look like,

Even though the repeated code is just two lines, it could be a maintenance headache. If the field is renamed, or its control type is switched from TextBox to DropDownList, there will be multiple places to be modified.

Here is the tip: Put the code snippet in a page extension method, so that it can be reused.


You may define the extension method in the control cs or vb file, below the section 1 region, and above all ISD-generated control classes. Below is an example of C# version,

Using the extension method could not be simpler.


Reusing code snippet with page extension methods makes your code easier to read and maintain. Because now you only need to modify one place for renaming or switching control type.