473,394 Members | 1,715 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,394 software developers and data experts.

Itemdatabound and edititemtemplate

I am trying to access a control from edititemtemplate in itemdatabound
event, but I get a run time null reference exception.

Class file :
In Datagrid_update I am able to access with no issues.
DataGridItem item = DataGrid2.Items[DataGrid2.EditItemIndex];
string test= ((TextBox) item.Cells[2].FindControl("xxx")).Text;

In DataGrid2_ItemDataBound , the following line gives me a run time
exception
string test1= ((TextBox) e.Item.Cells[2].FindControl("xxx)).Text;

Part of html:
<asp:TemplateColumn HeaderText="TotalTest">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Totalx") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="xxx" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Totalx") %>' />
</EditItemTemplate>
</asp:TemplateColumn>

Is edititemtemplate is not accessible from itemdatabound ? Any help is
appreicated.

Apr 4 '07 #1
2 4440
The OnItemDataBoundEvent fires for a lot of elements and containers. That
means that there could be a case were FindControl("xxx") returns null. So
you should test first to see if the return value of FindControl is not null.
You could try this..

TextBox myTextBox = e.Item.Cells[2].FindControl("xxx") as TextBox;
if (myTextBox != null)
{
string test1= ((TextBox) e.Item.Cells[2].FindControl("xxx")).Text;
}

HTH

---
Konstantinos Pantos [ASP.NET MVP]
http://kostas.pantos.name

I am trying to access a control from edititemtemplate in itemdatabound
event, but I get a run time null reference exception.

Class file :
In Datagrid_update I am able to access with no issues.
DataGridItem item = DataGrid2.Items[DataGrid2.EditItemIndex];
string test= ((TextBox) item.Cells[2].FindControl("xxx")).Text;
In DataGrid2_ItemDataBound , the following line gives me a run time
exception
string test1= ((TextBox) e.Item.Cells[2].FindControl("xxx)).Text;
Part of html:
<asp:TemplateColumn HeaderText="TotalTest">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Totalx") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="xxx" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Totalx") %>' />
</EditItemTemplate>
</asp:TemplateColumn>
Is edititemtemplate is not accessible from itemdatabound ? Any help is
appreicated.

Apr 4 '07 #2
Howdy,

No need to reference cells collection, and the current's item type is
available via ItemType:

protected void DataGrid2_ItemDataBound (object sender, DataGridItemEventArgs
e)
{
DataGridItem item = e.Item;

if (item.ItemType == ListItemType.EditItem)
{
TextBox textBox = (TextBox)item.FindControl("xxx");
string value = textBox.Text;
}
}

Hope this helps
--
Milosz
"sr**************@gmail.com" wrote:
I am trying to access a control from edititemtemplate in itemdatabound
event, but I get a run time null reference exception.

Class file :
In Datagrid_update I am able to access with no issues.
DataGridItem item = DataGrid2.Items[DataGrid2.EditItemIndex];
string test= ((TextBox) item.Cells[2].FindControl("xxx")).Text;

In DataGrid2_ItemDataBound , the following line gives me a run time
exception
string test1= ((TextBox) e.Item.Cells[2].FindControl("xxx)).Text;

Part of html:
<asp:TemplateColumn HeaderText="TotalTest">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Totalx") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="xxx" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "Totalx") %>' />
</EditItemTemplate>
</asp:TemplateColumn>

Is edititemtemplate is not accessible from itemdatabound ? Any help is
appreicated.

Apr 4 '07 #3

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

Similar topics

1
by: George Durzi | last post by:
When my datagrid is in edit mode, one of my columns is edited using a drop down list. I'm able to bind the DropDownList to a DataSource when in edit mode. HOWEVER, I can't preset the...
15
by: NH | last post by:
How can I only display the textbox I have in an EditItemTemplate based on the value in another cell in the row? i.e I only want users to edit one of the cells based on the value in another...
2
by: Hans Merkl | last post by:
Hi, I am trying to use a user control as EditItemTemplate in a DataList. It loads fine but I can't figure out how to bind to the data of the DataList. Here is what I have got so far: ...
0
by: Sudheer Aalla | last post by:
Hi, When I try to reference the radiobuttonlist control in edititemtemplate in ItemDataBound event of datalist control I am getting NULL reference. I am trying to pre select the yes/no button...
0
by: Dan | last post by:
Issue making textbox visible based on specific input from a radio button list in an EditItemTemplate I want to setup a gridview that when in edit mode and when the user selects "Other" from a...
2
by: Steve Hershoff | last post by:
Hi everyone, I have a DataGrid with several TemplateColumns. One of these columns has an EditItemTemplate that contains an ASP.Net DropDownList. I'm catching this DropDownList's...
1
by: Nathan Sokalski | last post by:
I am trying to access a control in the EditItemTemplate of a DataList in a usercontrol of mine. In the ItemDataBound event I can simply use CType(e.Item.FindControl("ddlEditSection"),...
0
by: dch | last post by:
I have a GridView, and when I click Edit, it is firing the OnRowEditing event ,and I am setting the edititem index, but it keeps displaying the ItemTemplate fields instead of the EditItemTemplate. I...
2
by: Hillbilly | last post by:
Anybody have any sage advice on this frustrating "feature" of ASP.NET? I have a TextBox in the EditItemTemplate of a DataList I can't seem to find for some reason that is I believe related to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.