Overriding Style Properties The Spreadsheet


Overriding Style Properties ”The Spreadsheet Example

Overriding 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

graphics/f11hn01.jpg

Listing 11-1 contains the code for the Spreadsheet control.

Listing 11-1 Spreadsheet.cs
 usingSystem; 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 :

  • In its constructor, Spreadsheet assigns the value to the CellSpacing property of the base class. This propagates the value into the ControlStyle property. Spreadsheet does not return a constant value from the CellSpacing get accessor for reasons that we explain after this bulleted list. Spreadsheet throws an exception from the CellSpacing set accessor to inform a user that this property cannot be assigned to. In addition, Spreadsheet applies design-time attributes to the CellSpacing property that tell the designer that the property is not browsable and that it should be hidden from the designer's serialization mechanism.

  • Overrides the GridLines property and reapplies the DefaultValue-Attribute to specify the new default value, GridLines.Both . In addition, in its constructor, Spreadsheet assigns the new default value to the GridLines property of the base class. This propagates the new default value into the ControlStyle property.

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> 


Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net