Monday, March 26, 2012

Strange Datagrid Update Problem

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!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.

-Brock
DevelopMentor
http://staff.develop.com/ballen

> 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!
Thanks Brock. I would never have guessed that whitespace would be a
member of the controls collection!

On Sat, 11 Jun 2005 07:06:34 -0700, Brock Allen
<ballen@.NOSPAMdevelop.com> wrote:

>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.
>-Brock
>DevelopMentor
>http://staff.develop.com/ballen

0 comments:

Post a Comment