Overriding Style Properties ”The Spreadsheet ExampleOverriding style properties is similar to overriding other properties. The main point to keep in mind is that changes that you make to property values must be propagated into the ControlStyle property of your control. We'll demonstrate how to override style properties by implementing a Spreadsheet control that derives from the ASP.NET Table control . Spreadsheet overrides the CellSpacing and GridLines properties so that the CellSpacing is always and the GridLines are rendered by default. This renders a table that resembles a spreadsheet with collapsed cell borders, as shown in Figure 11-1. Figure 11-1. The SpreadsheetTest.aspx page that uses the Spreadsheet control
Listing 11-1 contains the code for the Spreadsheet control. Listing 11-1 Spreadsheet.csusingSystem; usingSystem.ComponentModel; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; namespaceMSPress.ServerControls{ publicclassSpreadsheet:Table{ publicSpreadsheet(){ base.CellSpacing=0; base.GridLines=GridLines.Both; } [ Bindable(false), Browsable(false), DefaultValue(0), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), EditorBrowsable(EditorBrowsableState.Never) ] publicoverrideintCellSpacing{ get{ returnbase.CellSpacing; } set{ thrownewNotSupportedException("CannotsetCellSpacing"); } } [ DefaultValue(GridLines.Both) ] publicoverrideGridLinesGridLines{ get{ returnbase.GridLines; } set{ base.GridLines=value; } } } } The Spreadsheet control demonstrates how to override style properties by performing the following tasks :
By setting the CellSpacing and GridLines properties of the base class in its constructor, Spreadsheet propagates the new values into the ControlStyle property, which is responsible for state management and for the rendering of style properties. If you do not propagate changes into ControlStyle , the overridden style property will not render as expected. Listing 11-2 contains a page that uses the Spreadsheet control. Listing 11-2 SpreadsheetTest.aspx<%@PageLanguage="C#"%> <%@RegisterTagPrefix="msp"Namespace="MSPress.ServerControls" Assembly="MSPress.ServerControls"%> <html> <body> <formrunat="server"> <p>SpreadsheetstyletablerenderedbytheSpreadsheetcontrol: </p> <p> <msp:Spreadsheetid="spreadSheet1"runat="server" BorderWidth="1"BorderColor="Black"CellPadding="6"> <asp:TableRow> <asp:TableCellFont-Bold="True"Text="Item"> </asp:TableCell> <asp:TableCellFont-Bold="True" Text="Expenditure"></asp:TableCell> <asp:TableCellFont-Bold="True"Text="Comments"> </asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCellText="Books"></asp:TableCell> <asp:TableCellText="00.00"></asp:TableCell> <asp:TableCellText="Deductible"></asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCellText="Furniture"></asp:TableCell> <asp:TableCellText=",000.00"></asp:TableCell> <asp:TableCellText="Deductible"></asp:TableCell> </asp:TableRow> <asp:TableRow> <asp:TableCellText="Vacation"></asp:TableCell> <asp:TableCellText=",000.00"></asp:TableCell> <asp:TableCellText="Deductible?MentalHealth"> </asp:TableCell> </asp:TableRow> </msp:Spreadsheet> </p> </form> </body> </html> |