Strange Datagrid Update Problem

S

sosh

Hi,

I'm having a strange problem with the update function of a datagrid:

I have a datagrid, the first column lists item names from a database.
Second column is an EditCommandColumn, and then I have a couple of
button columns after that to deal with row deletes etc.

The problem is occuring in the method that I have specified should
deal with the update event:

protected void nameEditUpdate(object sender, DataGridCommandEventArgs
e)

Because the first column I know contains the item that is getting
updated, I try:

tmpTextBox = (TextBox)e.Item.Cells[0].Controls[0];

To get the control, before looking at its Text property. However,
when I try this I get:

System.InvalidCastException: Specified cast is not valid.

So to try and figure out what is going on, I have tried looking at the
type being returned by e.Item.Cells[0].Controls[0], which turns out to
be type System.Web.UI.LiteralControl, the name and text properties of
it are blank. This has just confused me even more.

Anyone have any idea just what might be going on?

Appreciated!
 
B

Brock Allen

Use FindControl instead of hard coding the position in the Controls collection.
Otherwise iterate over the Controls collection until you find your TextBox
(I bet it's at position Controls[1]). What's happening is that there's whitespace
in your template after the <ItemTemplate> and before your <asp:TextBox> and
that's the LiteralControl you're seeing.
 
S

Sosh

Thanks Brock. I would never have guessed that whitespace would be a
member of the controls collection!
 

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,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top