Problem with databinding in ITemplate class

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();
}
}
}
}
}
}
 
D

Duy Lam

DC said:
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();
}
}
}
}
}
}

Because class name is HeaderTextSort, i think your idea uses this
template in GridView's HeaderTemplate. Your code should be like this :
gridView.HeaderTemplate = new HeaderTextSort()
So what's your code in code behind of webpage ?
 
D

DC

DC said:
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-();
}
}
}
}
}
}

Because class name is HeaderTextSort, i think your idea uses this
template in GridView's HeaderTemplate. Your code should be like this :
gridView.HeaderTemplate = new HeaderTextSort()
So what's your code in code behind of webpage ?

Thank you, Duy. Yes, you are right, I was trying to create a
HeaderTemplate. This is how I loaded it:

Type t = Type.GetType("Templates.HeaderTextSort");
object o = Activator.CreateInstance(t);
myGridView.HeaderTemplate = (ITemplate)o;

When I use the Template as myGridView.ItemTemplate instead, then the
code works (DataItemIndex != -1 and DataItem != null). So I now know
what my mistake was: the HeaderTemplate is not databound or at least
not in the way the ItemTemplate is. I amtherefore looking for a way to
at least find out which Column Number I am in while processing the
HeaderItem.

Anyway, I am still working ont this approach: www.developerfusion.co.uk/show/4721
which looks good. It needs some modifications for GridViews:

public TemplateField LoadGridViewTemplateField(string path)
{
Control c = Page.LoadControl(path);
GridView g = c.Controls[0] as GridView;
if (g == null)
throw new Exception("Required GridView control not
found as the first child of specified template");

TemplateField f = g.Columns[0] as TemplateField;
if (f == null)
throw new Exception("Required TemplateField not found
as the first child of the specified GridView");

return f;
}

And the ascx looks like this:

<%@ Control %>
<asp:GridView ID="GridView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="TextBox1" runat="server" Text=<%#
Bind("DataField") %> />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

This opens up quiet some possibilities: specifying all or some
templates in one ascx, loading some or all templates from one ascx
file. Exactly what I was looking for.

I am still looking for a way to make headers with properties that
depend on the column number though.

Regards
DC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top