D
DC
Hi,
I am trying to implement a simple template control (to be used as
TemplateItem etc. in GridView). Does someone see why my code fails? I
give up for now and try a different approach (http://
www.developerfusion.co.uk/show/4721).
TIA for any ideas.
Regards
DC
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Templates
{
public class HeaderTextSort : ITemplate
{
public void InstantiateIn(Control container)
{
Label headerLabel = new Label();
headerLabel.DataBinding += new
EventHandler(headerLabel_DataBinding);
container.Controls.Add(headerLabel);
}
void headerLabel_DataBinding(object sender, EventArgs e)
{
if (sender is Label)
{
Label target = (Label)sender;
if (target.NamingContainer is GridViewRow)
{
// PROBLEM: DataItemIndex always -1, DataItem
always null
int index =
((GridViewRow)target.NamingContainer).DataItemIndex;
object data =
((GridViewRow)target.NamingContainer).DataItem;
if (data is DataRowView)
{
DataRowView row = (DataRowView)data;
target.Text =
row.DataView.Table.Columns[index].ExtendedProperties["HeaderText"].ToString();
}
}
}
}
}
}
I am trying to implement a simple template control (to be used as
TemplateItem etc. in GridView). Does someone see why my code fails? I
give up for now and try a different approach (http://
www.developerfusion.co.uk/show/4721).
TIA for any ideas.
Regards
DC
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace Templates
{
public class HeaderTextSort : ITemplate
{
public void InstantiateIn(Control container)
{
Label headerLabel = new Label();
headerLabel.DataBinding += new
EventHandler(headerLabel_DataBinding);
container.Controls.Add(headerLabel);
}
void headerLabel_DataBinding(object sender, EventArgs e)
{
if (sender is Label)
{
Label target = (Label)sender;
if (target.NamingContainer is GridViewRow)
{
// PROBLEM: DataItemIndex always -1, DataItem
always null
int index =
((GridViewRow)target.NamingContainer).DataItemIndex;
object data =
((GridViewRow)target.NamingContainer).DataItem;
if (data is DataRowView)
{
DataRowView row = (DataRowView)data;
target.Text =
row.DataView.Table.Columns[index].ExtendedProperties["HeaderText"].ToString();
}
}
}
}
}
}