ho fatto qualcosa, nel senso che non mi da piu' errore se trova <%#VARIABILE%> però se setto una proprietà tipo HeaderText='A<%#"a"%>'
lui mi scrive A<%#"a"%> mentre se metto solo <%#"a"%> mi taglia il contenuto del Tag..
credo che da qualche parte dovrei fare un DataBinder.eval, ma non mi capisco piu':
ho fatto l'overloading dei metodi necessari utilizzando il Control di Mono come fonte di ispirazione...
l'unica cosa che non capisco e come fare a riempirmi l'interfaccia che mi serve!!!
codice:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Web.Util;
using System.ComponentModel;
namespace MyDataBound
{
public class BindableDataBound : DataGridColumn ,System.Web.UI.IDataBindingsAccessor
{
private static readonly object DataBindingEvent = new object();
private EventHandlerList _events = new EventHandlerList();
public ICollection DataSource;
public string DataField;
public string DataTextField;
public string DataValueField;
private DataBindingCollection dataBindings;
private bool bindingContainer = true;
private bool _isNamingContainer = false;
private DataGrid _namingContainer = null;
private DataGrid _parent = null;
public DataGrid BindingContainer
{
get
{
DataGrid container = NamingContainer;
return container;
}
}
public virtual DataGrid NamingContainer
{
get
{
if (_namingContainer == null && _parent != null)
{
_namingContainer = _parent;
}
return _namingContainer;
}
}
DataBindingCollection IDataBindingsAccessor.DataBindings
{
get
{
if(dataBindings == null)
dataBindings = new DataBindingCollection();
return dataBindings;
}
}
bool IDataBindingsAccessor.HasDataBindings
{
get
{
return (dataBindings!=null && dataBindings.Count>0);
}
}
public override void InitializeCell(TableCell cell,int columnIndex,ListItemType itemType)
{
base.InitializeCell(cell,columnIndex,itemType);
switch (itemType)
{
case ListItemType.Header:
if (this.dataBindings!=null &&this.dataBindings.Count>0)
if(this.dataBindings["HeaderText"]!=null)
this.HeaderText=this.dataBindings["HeaderText"].ToString();
cell.Text = this.HeaderText;
break;
case ListItemType.Item:
case ListItemType.AlternatingItem:
cell.DataBinding += new EventHandler(this.ItemDataBinding);
break;
case ListItemType.EditItem:
cell.DataBinding += new EventHandler(this.EditItemDataBinding);
cell.Controls.Add(new TextBox());
break;
}
}
private void ItemDataBinding(object sender,EventArgs e)
{
TableCell cell = sender as TableCell;
if (cell == null)
throw new Exception("Table Cell was not found");
DataGridItem gridItem = cell.NamingContainer as DataGridItem;
if (gridItem == null)
throw new Exception("Data Grid Item was not found");
try
{
cell.Text = DataBinder.Eval(gridItem.DataItem,this.DataField).ToString();
}
catch (IndexOutOfRangeException rangeEx)
{
throw new Exception("Specified DataField was not found.");
}
catch (Exception ex)
{
throw new Exception(ex.InnerException.ToString());
}
}
private void EditItemDataBinding(object sender,EventArgs e)
{
TableCell cell = sender as TableCell;
if (cell == null)
throw new Exception("Table Cell was not found");
DataGridItem gridItem = cell.NamingContainer as DataGridItem;
if (gridItem == null)
throw new Exception("Data Grid Item was not found");
TextBox txt = cell.Controls.Count == 0 ? null : cell.Controls[0] as TextBox;
if (txt == null)
throw new Exception("Text Box was not found");
foreach(object item in this.DataSource)
{
if (item == null)
{
throw new Exception("Invalid DataSource");
}
else if (item.GetType() == typeof(string))
{
txt.Text=((string)item);
}
else
{
throw new Exception("Invalid DataSource");
}
}
}
public event EventHandler DataBinding //DIT
{
add
{
Events.AddHandler(DataBindingEvent, value);
}
remove
{
Events.RemoveHandler(DataBindingEvent, value);
}
}
protected EventHandlerList Events //DIT
{
get
{
if (_events == null)
{
_events = new EventHandlerList();
}
return _events;
}
}
public BindableDataBound():base()
{
//
// TODO: Add constructor logic here
//
}
}
}