473,779 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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();
}
}
}
}
}
}
Nov 16 '07 #1
2 4919
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
Nov 16 '07 #2
DC
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
Nov 19 '07 #3

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

Similar topics

0
2406
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 CheckBox Programmatically" in my code. I have changed it to use a OLDDB.DBReader to populate the datagrid and for the databinding. It works perfectly - also when using the edit-mode in the datagrid.
0
1833
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 it's a Sharepoint WebPart. I'm trying to do something fairly simple, create a datagrid that displays where and when a person works and allows them to change some of the information via DropDownLists. When the user clicks to edit a row, three of...
1
2821
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 codehind file and I don't know how to get it work since I did not find any equivalent for "Bind" to use in my ITemplate derived class. For my ItemTemplate, I use (int)((DataRowView)container.DataItem) as equivalent to <%# Eval ...%>
2
4704
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
9356
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 GridView.Visible = True. I press HIDE and the GridView disappears as expected. After it I press SHOW and the GridView doesn't show.
1
7314
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 implements ITemplate and repopulate the Template properters of TemplateField in OnInit. And I am DataBinding by getting a DataTable from my db provider class in Page_Load event. When I databind programmatically in the Page_Load, the data displays in...
3
3065
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=" & System.Threading.Thread.CurrentThread.CurrentCulture.ToString() I believe {0} retrieves the value of my datasource column with index 0.
0
1192
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 record I used datalist. And becoz of query return different no of column based on condtion I created template class for design and itembinding at run time . I manage to create template column() but confuse over how to bind data. As no of column...
4
9024
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 columns. I am using a code which adds a checkbox for each column: public void InstantiateIn(System.Web.UI.Control container) { switch (templateType)
0
10302
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10136
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6723
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2867
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.