Home All Groups Group Topic Archive Search About

Can't get a reference to user control in datagrid

Author
9 Mar 2005 2:03 PM
Don
I created a Web User Control in my project and need to use it in a
datagrid.  The data in this control needs to be updated and the control
has several properties that need to be databound.

Using the designer I created a a dataGrid with an Edit, Update, Cancel
column, and an empty template column.

I have overridden ItemDataBound to instantiate my control, set the
properties using DataItems, and add this control to the template column
in the grid.  This part works and i see the data.  When the users
clciks edit they get an editable version of my control which also work
as expected.

When the users clicks update I need to read the data from my control
and save it to the database.  The problem is I cannot find the control.
It should be in e.Item.Cells(1) but there are no controls in the cell.
The only other Cells is 0 and it is not here.

How do I reference my User Control?

Don

Author
9 Mar 2005 4:49 PM
Elton Wang
Hi Don,

You can try e.FindControl("your control ID") to get the
reference.

HTH

Elton Wang
elton_w***@hotmail.com


>-----Original Message-----
>I created a Web User Control in my project and need to
use it in a
>datagrid.  The data in this control needs to be updated
and the control
>has several properties that need to be databound.
>
>Using the designer I created a a dataGrid with an Edit,
Update, Cancel
>column, and an empty template column.
>
>I have overridden ItemDataBound to instantiate my
control, set the
>properties using DataItems, and add this control to the
template column
>in the grid.  This part works and i see the data.  When
the users
>clciks edit they get an editable version of my control
which also work
>as expected.
>
>When the users clicks update I need to read the data from
my control
>and save it to the database.  The problem is I cannot
find the control.
> It should be in e.Item.Cells(1) but there are no
controls in the cell.
Show quoteHide quote
> The only other Cells is 0 and it is not here.
>
>How do I reference my User Control?
>
>Don
>
>.
>
Author
9 Mar 2005 6:50 PM
Don
e does not have a FindControl method but e.item does.  I tried it but
it does not work.
Author
9 Mar 2005 9:06 PM
Elton Wang
My mistake.

It's

e.Item.FindControl("your control ID")


>-----Original Message-----
>e does not have a FindControl method but e.item does.  I
tried it but
Show quoteHide quote
>it does not work.
>
>.
>
Author
9 Mar 2005 9:49 PM
Tim Stall
Hello Don,

You should be able to reference the control like:
    UCSimple uc =
(UCSimple)this.DataGrid1.Items[intRow].Cells[_UCColumn].Controls[_UCControlPosition];

or from within the grid itself:
    UCSimple uc = (UCSimple)e.Item.Cells[_UCColumn].Controls[_UCControlPosition];
where _UCOlumn refers to the column, and _UCControlPosition refers to the
position in the cell.

I recently wrote a blog about this:
http://timstall.dotnetdevelopersjournal.com/read/1121938.htm
including a simple download code sample that has a UC and host page.

> It should be in e.Item.Cells(1)

The structure sounds right. Can you see the control present in the grid? Can
you reference it absolutely (i.e. without respect to the specific event 'e')?
In other words, maybe there's a problem in putting the control in the grid,
not referencing it.



Show quoteHide quote
"Don" wrote:

> I created a Web User Control in my project and need to use it in a
> datagrid.  The data in this control needs to be updated and the control
> has several properties that need to be databound.
>
> Using the designer I created a a dataGrid with an Edit, Update, Cancel
> column, and an empty template column.
>
> I have overridden ItemDataBound to instantiate my control, set the
> properties using DataItems, and add this control to the template column
> in the grid.  This part works and i see the data.  When the users
> clciks edit they get an editable version of my control which also work
> as expected.
>
> When the users clicks update I need to read the data from my control
> and save it to the database.  The problem is I cannot find the control.
>  It should be in e.Item.Cells(1) but there are no controls in the cell.
>  The only other Cells is 0 and it is not here.
>
> How do I reference my User Control?
>
> Don
>
>