1: using System;
2: using System.Drawing;
3: using System.Drawing.Design;
4: using System.Collections;
5: using System.Collections.Generic;
6: using System.ComponentModel;
7: using System.Security.Permissions;
8: using System.Web;
9: using System.Web.UI;
10: using System.Web.UI.WebControls;
11:
12: namespace CustomDataBoundControls
13: {
14: [
15: AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
16: AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal),
17: DefaultProperty("DataFields"),
18: ParseChildren(true,"DataFields"),
19: ToolboxData("<{0}:DataBoundTable runat=\"server\" ShowHeaders=\"true\" > </{0}:DataBoundTable>"),
20: ToolboxBitmap(typeof(DataBoundTable), "logo")
21: ]
22: public class DataBoundTable : DataBoundControl
23: {
24: private List<DataField> dataFields = new List<DataField>();
25:
26: // This property keeps the DataFields. DataFields are the fields
27: // shown by this custom table. If no DataField is defined, the control
28: // shows all the fields available in the given DataSource.
29: [
30: System.ComponentModel.Browsable(true),
31: System.ComponentModel.Category("Data"),
32: DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
33: Editor(typeof(DataFieldCollectionEditor), typeof(UITypeEditor)),
34: PersistenceMode(PersistenceMode.InnerDefaultProperty)
35: ]
36: public List<DataField> DataFields
37: {
38: get { return dataFields; }
39: set { dataFields = value; }
40: }
41:
42: // This property decides whether the headers should be shown or not.
43: [System.ComponentModel.Browsable(true),
44: System.ComponentModel.Category("Appearance"),
45: DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
46: PersistenceMode(PersistenceMode.Attribute)
47: ]
48: public bool ShowHeaders
49: {
50: get { return _showHeaders; }
51: set { _showHeaders = value; }
52: }
53: private bool _showHeaders;
54:
55:
56: protected override void PerformSelect()
57: {
58: // Call OnDataBinding here if bound to a data source using the
59: // DataSource property (instead of a DataSourceID), because the
60: // databinding statement is evaluated before the call to GetData.
61: if (!IsBoundUsingDataSourceID)
62: {
63: this.OnDataBinding(EventArgs.Empty);
64: }
65:
66: // The GetData method retrieves the DataSourceView object from
67: // the IDataSource associated with the data-bound control.
68: GetData().Select(CreateDataSourceSelectArguments(),
69: this.OnDataSourceViewSelectCallback);
70:
71: // The PerformDataBinding method has completed.
72: RequiresDataBinding = false;
73: MarkAsDataBound();
74:
75: // Raise the DataBound event.
76: OnDataBound(EventArgs.Empty);
77: }
78:
79: private void OnDataSourceViewSelectCallback(IEnumerable retrievedData)
80: {
81: // Call OnDataBinding only if it has not already been
82: // called in the PerformSelect method.
83: if (IsBoundUsingDataSourceID)
84: {
85: OnDataBinding(EventArgs.Empty);
86: }
87: // The PerformDataBinding method binds the data in the
88: // retrievedData collection to elements of the data-bound control.
89: PerformDataBinding(retrievedData);
90: }
91:
92: protected override void PerformDataBinding(IEnumerable retrievedData)
93: {
94: base.PerformDataBinding(retrievedData);
95:
96: // Verify data exists. retrievedData variable will hold all the
97: // rows retrieved from the data source.
98: if (retrievedData != null)
99: {
100: // Creating the table which shows the data.
101: // This is the main appearance of this custom control.
102: Table tbl = new Table();
103:
104: if (ShowHeaders)
105: {
106: // creating the header row of the table if ShowHeader is true
107: TableRow headerRow = new TableRow();
108: TableCell headerCell;
109: tbl.Rows.Add(headerRow);
110:
111: // if DataFields are mentioned (i.e. the developer has configured
112: // which columns to be shown) then using the Key property as the
113: // header of those chosen columns
114: if (DataFields.Count > 0)
115: {
116: foreach (DataField field in DataFields)
117: {
118: headerCell = new TableCell();
119: headerCell.Text = field.Header;
120: headerCell.Font.Bold = true;
121: headerRow.Cells.Add(headerCell);
122: tbl.Rows.Add(headerRow);
123: }
124: }
125:
126: // DataFields are not mentioned. Showing header for all columns
127: // available in DataSource. Using the value of Key property as
128: // the header.
129: else
130: {
131: IEnumerator myEnumerator = retrievedData.GetEnumerator();
132: myEnumerator.MoveNext();
133:
134: PropertyDescriptorCollection props =
135: TypeDescriptor.GetProperties(myEnumerator.Current);
136:
137: for (int i = 0; i < props.Count; i++)
138: {
139: headerCell = new TableCell();
140: headerCell.Text = props[i].Name;
141: headerCell.Font.Bold = true;
142: headerRow.Cells.Add(headerCell);
143: }
144: }
145: }
146:
147:
148: // creating the rows containing data...
149:
150: TableRow row;
151: TableCell cell;
152: string dataStr = String.Empty;
153:
154: foreach (object dataItem in retrievedData)
155: {
156: row = new TableRow();
157: tbl.Rows.Add(row);
158:
159: // if DataFields are mentioned (i.e. the developer has configured
160: // which columns to be shown) then using the Value property as the
161: // Value of those chosen columns
162: if( DataFields.Count > 0)
163: {
164: foreach (DataField field in DataFields)
165: {
166: // field.FieldName will provide the column name
167: // using it to get the data of that column for the current row.
168: dataStr = DataBinder.GetPropertyValue(dataItem,
169: field.FieldName, null);
170:
171: cell = new TableCell();
172: cell.Text = dataStr;
173: row.Cells.Add(cell);
174: }
175: }
176:
177: // DataFields are not mentioned. Showing value for all columns
178: // available in DataSource. Using the Value property as
179: // the value.
180: else
181: {
182: PropertyDescriptorCollection props =
183: TypeDescriptor.GetProperties(dataItem);
184: for (int i = 0; i < props.Count; i++)
185: {
186: if (props[i].GetValue(dataItem) != null)
187: {
188: dataStr = props[i].GetValue(dataItem).ToString();
189: cell = new TableCell();
190: cell.Text = dataStr;
191: row.Cells.Add(cell);
192: }
193: }
194: }
195: }
196:
197: this.Controls.Add(tbl);
198: }
199: }
200: }
201: }