Object-Oriented ASP.NET

Putting it All Together

Now comes the beautiful part. Once we have created our derived DataGrid class called ConfirmDelDataGrid, using it and leveraging its new built-in functionality is a snap. First, add a @Register tag on the .aspx page to register the datagrid class and use it rather than <asp:DataGrid>:

<%@ Register tagprefix="dg" Namespace="ooaspnet" Assembly="ooaspnet" %>
< !-- (page layout & design here)... -->
< dg:ConfirmDelDataGrid id="grid" runat="server" ..... ><dg:ConfirmDelDataGrid>

You'll also need to set the code-behind in your .cs file to use the new derived class: protected ConfirmDelDataGrid grid;

private void Page_Load(object sender, System.EventArgs e)
{
    if (!IsPostBack)
    {
        grid.ConfirmOnDelete = true;
        grid.DataSource = new string[4] { "red", "green", "blue", "purple" };
        grid.DataBind();
    }
}

This is as simple as it gets. We have a new DataGrid subclass with a property that lets us automatically add confirmation message boxes, without knowing (or caring) at the Page level how it works.

Also, if we ever decide to improve the code for attaching the javascript, we can just modify the code inside the ConfirmDelDataGrid class once. Every page that uses this class will automatically benefit from the changes.

Conclusion

Programming ASP.NET allows us to take advantage of the powerful features of the underlying languages that we use. We can use object-oriented design features such as inheritance and encapsulation to develop re-usable classes that work autonomously and do not require glue code on the Page objects.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” - Tom Cargill