473,385 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with databinding in ITemplate class

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).DataItemInde x;
object data =
((GridViewRow)target.NamingContainer).DataItem;

if (data is DataRowView)
{
DataRowView row = (DataRowView)data;
target.Text =
row.DataView.Table.Columns[index].ExtendedProperties["HeaderText"].ToString();
}
}
}
}
}
}
Nov 16 '07 #1
2 4901
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.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).DataItemInde x;
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 ?

--
Duy Lam Phuong
Nov 16 '07 #2
DC
On 16 Nov., 17:54, Duy Lam <duylamphu...@gmail.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.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).DataItemInde x;
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 ?

--
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("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
Nov 19 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a...
0
by: Daniel Doyle | last post by:
Hello and apologies in advance for the amount of code in this post. I've also sent this message to the Sharepoint group, but thought that ASP.NET developers may also be able to help, even though...
1
by: dieter | last post by:
Two-way databinding (as described in http://dotnetjunkies.com/QuickStartv20/aspnet/doc/data/templates.aspx) works fine for me if I use it within aspx-files. However, I would like to use it my...
2
by: Demetri | last post by:
Umm...this wont work Public Class MyTemplat Inherits ITemplat End Clas I get an error stating that classes can only inherit from classes C# allows you to do this simpl
1
by: Miguel Dias Moura | last post by:
Hello, I have a GridView in my page which is created in runtime. It works fine. My page has 2 Asp Buttons: - The HIDE button makes GridView.Visible = False; - The SHOW button makes...
1
by: Mark Stafford | last post by:
I am attempting to use a DetailsView control to view some data where the fields returned by the database are determined at runtime. I create the TemplateFields on the fly using a class that...
3
by: shapper | last post by:
Hello, In my GridView I have a HyperLink Field where I set the DataNavigateUrlFormaString MyHyperLinkField.DataNavigateUrlFormatString = "~\RSS.ashx?Channel={0}&Culture=" &...
0
by: imranabdulaziz | last post by:
Hi all, I am using asp.net and C# Visual studio 2005. Let me explain the scenario. I have stored procedure which return very no of column based on condition. Becoz I have to show columnwise...
4
by: beton3000 | last post by:
Hello! I'm building a GridView using code to add template fields, because there is a random number of columns in result set from database. I'm using a class with ITemplate interface for defining...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.