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.
Comments