interactive | editorial | code | resource 
FAQ > Repeated Code
 

Repeated Code

Q: Is there a way to store the registration key, and other repeated properties in just one place in my application?

A:

Yes, in fact there's two easy ways to give all your DbCombo's the same initial values. This makes it much easier to manage a large application. In fact, these methods are not limited to DbCombo controls, you can use them on and WebControls - e.g. the DataGrid.

Method 1

To automate the process of setting the registration key property (or other properties), you can create an OnInit event for your DbDombo control, as in the code sample below:

<DbCombo:DbCombo runat=server id=Combo1 OnInit="AssignReg" />

<script runat=server>
    protected void AssignReg(object o, EventArgs e)
    {
        Cambro.Web.DbCombo combo = (Cambro.Web.DbCombo)o;
        combo.RegistrationKey = "[your-dev-key]"
    }
</script>

If you have many pages that use DbCombo, put the AssignReg in a new class that inherits from Page, and derive all your pages that use DbCombo from this class.

You can also set-up other properties e.g. styles in this function to save you from having to enter them each time.

Method 2

A neater solution is to create another class that derives from Cambro.Web.DbCombo - this will inherit all DbCombo functionality, and allow you to change the default properties. Create a class file in Visual Studio, and enter something like:

using System;

namespace [your-namespace]
{
    public class DbCombo : Cambro.Web.DbCombo.DbCombo
    {
        public DbCombo() : base()
        {
            this.RegistrationKey="[your-key]";
        }
    }
}

Then in your pages, chenge:

<%@ Register TagPrefix="DbCombo" Namespace="Cambro.Web.DbCombo"
Assembly="Cambro.Web.DbCombo" %>

to:

<%@ Register TagPrefix="DbCombo" Namespace="[your-namespace]"
Assembly="[your-assembly]" %>

You won't have to change the class that all your pages inherit from that way.


 
FAQ > Repeated Code