Getting data from child controls loaded programmatically
Posted
by Farinha
on Stack Overflow
See other posts from Stack Overflow
or by Farinha
Published on 2010-05-18T11:40:41Z
Indexed on
2010/05/18
12:00 UTC
Read the original article
Hit count: 263
I've created 2 controls to manipulate a data object: 1 for viewing and another for editing.
On one page I load the "view" User Control and pass data to it this way:
ViewControl control = (ViewControl)LoadControl("ViewControl.ascx");
control.dataToView = dataObject;
this.container.Controls.Add(control);
That's all fine and inside the control I can grab that data and display it.
Now I'm trying to follow a similar approach for editing. I have a different User Control for this (with some textboxes for editing) to which I pass the original data the same way I did for the view:
EditControl control = (EditControl)LoadControl("EditControl.ascx");
control.dataToEdit = dataObject;
this.container.Controls.Add(control);
Which also works fine.
The problem now is getting to this data. When the user clicks a button I need to fetch the data that was edited and do stuff with it. What's happening is that because the controls are being added programmatically, the data that the user changed doesn't seem to be accessible anywhere.
Is there a solution for this? Or is this way of trying to keep things separated and possibly re-usable not possible?
Thanks in advance.
© Stack Overflow or respective owner