Why I am Unable to read the content of a TemplateColumn ?

I

Ing. Rajesh Kumar

Hi everybody
I have a problem reading the content of a cell. When i use AutogenerateColumns = True or when i use
<asp:BoundColumn DataField="COLUMN1" HeaderText="COLUMN1"/> then i can simply read the content of a cell in the OnItemDataBound method using the following code :
For j = 0 To DT.Columns.Count - 1
If e.Item.Cells(j).Text = "something" Then Do Something
Next

Now i changed some columns to template columns as follows :
<asp:TemplateColumn HeaderText="COLUMN1">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "COLUMN1") %></ItemTemplate>
</asp:TemplateColumn>
and now i am unable to read the content of the Template Columns. It still reads BoundColumn's content. Last four hours i spend playing with it and still cannot read the value.

Just for testing i used the "nothing" comand as follows:
If e.Item.Cells(j).Text = "" Then Do Something
and it works. So it means that there is no value in the template column OnItemDataBound ?
Then how should i read the content of a TemplateColumn ?
Thanks in advance
Raja
 
N

Natty Gur

Hi,


Template controls should hold inner control/s. you should access them
(inner controls) by using findcontrol to get reference to the template
inner control.

by the way why do you use OnItemDataBound event which fire when data
binds into grid item to get data grid items data ?


Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 
I

Ing. Rajesh Kumar

Thanks Natty
I was missing the label control in the template column. I had some
True/False fields in my DataGrid and that's why i changed my DataGrid to use
Template columns so i could use checkboxes in it but for the display i used
this
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "AIT")
%></ItemTemplate>
<edititemtemplate><asp:CheckBox ID="chkEditAit" Checked=<%#
CheckBoxValueConverter(Container.DataItem("AIT")) %>
runat="server"/></edititemtemplate>
instead of this
<ItemTemplate><asp:Label ID="Label_4" Text=<%#
DataBinder.Eval(Container.DataItem, "AIT") %>
runat="server"/></ItemTemplate>
<edititemtemplate><asp:CheckBox ID="chkEditAit" Checked=<%#
CheckBoxValueConverter(Container.DataItem("AIT")) %>
runat="server"/></edititemtemplate>

The result was fine so i was fixed to find the problem elsewhere :)

Now, why I am using OnItemDataBound :
Well, OnItemDataBound I am checking the values of all the fields and
depending on those values i am changing the display property of the given
Cell. For example if a cell value is less than some given value then Cell's
background is red, etc.
Thanks a lot.
Raja
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top