Completing Task Two in the Sample Application


We've hardly talked about task two. Task two in the sample application involves attributes, and your goal is to create a custom attribute. As you might have guessed from the previous sections, you're going to define an attribute called VisibleColumnAttribute. The idea is to create a function that automatically configures a grid control according to the attributes that have been added to a class. So if the grid is going to have inventory items, we can pass the grid and the InventoryItem class to a function and the function will look at the attributes and format the grid according to the attributes.

To finish task two in the sample application:

  1. In the sample code directory for Chapter 12, find the subdirectory called ordersInterfaces. In there you'll find a file called ordersInterfaces.sln. Double-click on this file to open it with VS.NET.

  2. If you look at the Solution Explorer you'll see that the solution has three projects in it. We're going to be working with the files in the ordersInterfaces project highlighted in bold ( Figure 12.35 ).

    Figure 12.35. The ordersInterfaces solution file has three projects in one. They're each stored in a different subdirectory. Grouping projects into one solution makes it easier to work with multiple related projects.

    graphics/12fig35.gif

  3. Open the file interfacedef.cs.

  4. Inside the namespace declaration before the definition of ICustomer add the code in Figure 12.36 . This code defines the VisibleColumnAttribute class.

    Figure 12.36 The VisibleColumn attribute can be applied to properties. It tells our grid function whether we want the property information displayed or not.
     [AttributeUsage(AttributeTargets.Property)] public class VisibleColumnAttribute : System.Attribute {    public bool Visible;    public VisibleColumnAttribute(bool value)    {       Visible=value;    } } 
  5. Now that you've defined the attribute, we can apply it to some of the properties in a class. Open the file supportclasses.cs.

  6. In the definition of Customer locate the properties ID and Name. Above ID apply the VisibleColumn attribute with a value of false, and above Name apply it again with a value of true ( Figure 12.37 ).

    Figure 12.37 Notice that it's very easy for a developer of a class to specify whether a column should be displayed or not.
     public class Customer : ICustomer {    string _id;    string _name;  [VisibleColumn(false)]  public string ID    {       get       {          return _id;       }       set       {          _id = value;       }    }  [VisibleColumn(true)]  public string Name    {       get       {          return _name;       }       set       {          _name = value;       }    } } 
  7. Build the application to make sure everything is in order.

  8. Open the OrdersSystem solution file (OrdersSystem/OrderSystem.sln).

  9. Open the file customers.aspx.

  10. Before the Page_Load function, add the code in Figure 12.38 . It defines the PrepareCustomerGrid that searches for the attribute and configures the grid control to display columns or hide columns depending on the attribute settings.

    Figure 12.38 PrepareCustomerGrid uses reflection to find all the properties in the customer class and adds a column to the grid for each one. It then looks for a VisibleColumn attribute to see if it should make the column visible or not.
     void PrepareCustomerGrid() {    Type typeCustomer =    typeof(ordersInterfaces.Customer);    grdCustomers.Columns.Clear();    foreach(PropertyInfo prop in           typeCustomer.GetProperties())    {       BoundColumn grdcol =       new BoundColumn();       bool visible = false;       if (prop.IsDefined(       typeof(VisibleColumnAttribute),       false))       {          object[] attrs =          prop.GetCustomAttributes(          typeof(ordersInterfaces.          VisibleColumnAttribute),          false);          VisibleColumnAttribute viscol =         (VisibleColumnAttribute)          attrs[0];          visible = viscol.Visible;       }       grdcol.HeaderText = prop.Name;       grdcol.DataField = prop.Name;       grdcol.Visible = visible;       grdCustomers.Columns.Add(grdcol);    }    HyperLinkColumn linkcol =    new HyperLinkColumn();    linkcol.Text = "View Orders";    linkcol.DataNavigateUrlField = "ID";    linkcol.DataNavigateUrlFormatString=                 "orders.aspx?ID={0}";    grdCustomers.Columns.Add(linkcol);    ButtonColumn buttoncol =    new ButtonColumn();    buttoncol.Text = "Delete";    buttoncol.CommandName = "Delete";    buttoncol.HeaderText = "Delete";    grdCustomers.Columns.Add(buttoncol); } 
  11. Inside the function Page_Load before the line that reads RefreshGrid add a call to the PrepareCustomerGrid function.

graphics/tick.gif Tip

  • It seems to me that with more work it would be possible to create a nice function that could format any grid based on attributes you set in the class. You could for example define attributes to control colors, type of cell , width of column, whether the column is editable or not, and drive a lot of the functionality with attributes.




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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