On 16 Nov., 17:54, Duy Lam <duylamphu...@g mail.comwrote:
DC wrote:
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.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
namespace Templates
{
public class HeaderTextSort : ITemplate
{
public void InstantiateIn(C ontrol container)
{
Label headerLabel = new Label();
headerLabel.Dat aBinding += new
EventHandler(he aderLabel_DataB inding);
container.Contr ols.Add(headerL abel);
}
void headerLabel_Dat aBinding(object sender, EventArgs e)
{
if (sender is Label)
{
Label target = (Label)sender;
if (target.NamingC ontainer is GridViewRow)
{
// PROBLEM: DataItemIndex always -1, DataItem
always null
int index =
((GridViewRow)t arget.NamingCon tainer).DataIte mIndex;
object data =
((GridViewRow)t arget.NamingCon tainer).DataIte m;
if (data is DataRowView)
{
DataRowView row = (DataRowView)da ta;
target.Text =
row.DataView.Ta ble.Columns[index].ExtendedProper ties["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.Header Template = new HeaderTextSort( )
So what's your code in code behind of webpage ?
--
Duy Lam Phuong- Zitierten Text ausblenden -
- Zitierten Text anzeigen -
Thank you, Duy. Yes, you are right, I was trying to create a
HeaderTemplate. This is how I loaded it:
Type t = Type.GetType("T emplates.Header TextSort");
object o = Activator.Creat eInstance(t);
myGridView.Head erTemplate = (ITemplate)o;
When I use the Template as myGridView.Item Template 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 LoadGridViewTem plateField(stri ng path)
{
Control c = Page.LoadContro l(path);
GridView g = c.Controls[0] as GridView;
if (g == null)
throw new Exception("Requ ired GridView control not
found as the first child of specified template");
TemplateField f = g.Columns[0] as TemplateField;
if (f == null)
throw new Exception("Requ ired 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:TemplateFi eld>
<ItemTemplate >
<asp:Label ID="TextBox1" runat="server" Text=<%#
Bind("DataField ") %/>
</ItemTemplate>
</asp:TemplateFie ld>
</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