using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using System.Text;
namespace WebPractice
{
/// <summary>
/// Summary description for AddressColumn.
/// </summary>
public class AddressColumn:BoundColumn
{
#region Constructor
public AddressColumn()
{
//
// TODO: Add constructor logic here
//
}
#endregion
#region Variables
private string _strCssClass;
#endregion
#region Public Property
public string CssClass
{
get
{
return _strCssClass;
}
set
{
_strCssClass = value;
}
}
#endregion
public override void InitializeCell(TableCell cell, int columnIndex, ListItemType itemType)
{
base.InitializeCell (cell, columnIndex, itemType);
switch(itemType)
{
case ListItemType.Item:
cell.DataBinding += new EventHandler(this.CellItemDataBound);
break;
case ListItemType.AlternatingItem:
cell.DataBinding += new EventHandler(this.CellItemDataBound);
break;
case ListItemType.Footer:
cell.DataBinding += new EventHandler(this.CellItemDataBound);
break;
}
}
#region Methods
private void CellItemDataBound(object s,EventArgs e)
{
TableCell tblCell = (TableCell)s;
DataGridItem dgiAge = (DataGridItem)tblCell.NamingContainer;
Employee.Address _adr = new WebPractice.Employee.Address();
StringBuilder _sbHtmlTag;
switch(dgiAge.ItemType)
{
case ListItemType.Item:
_adr = this.GetUnderlyingValue(dgiAge.DataItem);
_sbHtmlTag = new StringBuilder();
if(_adr.Area != null && _adr.Area.Trim() != "")
{
_sbHtmlTag.Append("<div class='" + CssClass + "'>");
_sbHtmlTag.Append("Area: ");
_sbHtmlTag.Append(_adr.Area);
_sbHtmlTag.Append("</div>");
}
if(_adr.City != null && _adr.City.Trim() != "")
{
_sbHtmlTag.Append("<div class='" + CssClass + "'>");
_sbHtmlTag.Append("City: ");
_sbHtmlTag.Append(_adr.City);
_sbHtmlTag.Append("</div>");
}
tblCell.Text = _sbHtmlTag.ToString();
break;
case ListItemType.AlternatingItem:
_adr = this.GetUnderlyingValue(dgiAge.DataItem);
_sbHtmlTag = new StringBuilder();
if(_adr.Area != null && _adr.Area.Trim() != "")
{
_sbHtmlTag.Append("<div class='" + CssClass + "'>");
_sbHtmlTag.Append("Area: ");
_sbHtmlTag.Append(_adr.Area);
_sbHtmlTag.Append("</div>");
}
if(_adr.City != null && _adr.City.Trim() != "")
{
_sbHtmlTag.Append("<div class='" + CssClass + "'>");
_sbHtmlTag.Append("City: ");
_sbHtmlTag.Append(_adr.City);
_sbHtmlTag.Append("</div>");
}
tblCell.Text = _sbHtmlTag.ToString();
break;
case ListItemType.Footer:
break;
}
}
protected Employee.Address GetUnderlyingValue(object dataItem)
{
PropertyDescriptor _prpDesc = TypeDescriptor.GetProperties(dataItem).Find(this.DataField,true);
if(_prpDesc == null)
{
throw new HttpException("Field Not Found: " + this.DataField);
}
return ((Employee.Address)_prpDesc.GetValue(dataItem));
}
#endregion
}
}
|