Dynamic Control in an Assembly

  • Thread starter Thread starter Lil' Dub
  • Start date Start date
L

Lil' Dub

I've written an assembly the dynamically generates a Table control and
returns it. But, I can't seem to get it to draw on the page. It's called
from the Page_Load and I've tried setting the ID of the Table control on the
page the same name as the one returned from the assembly and I've tried
assigning it to a new Table control on the page like:

newTable = obj.GetTable();

But neither method works. Has anyone accomplished this?

Thanks
 
Try adding a placeholder control to your page (in both the declarative and
imperative code) and adding the object to the controls collection of this
placeholder.
 
Great. That worked. Why is it though that you can't transfer a Table object
to another Table object?
 
The reason what you were doing is that you weren't succeeding in adding the
new table object to the controls collection of the derived page class. If
you just set the id attribute to be equal, you still just have a table
object sitting in memory but not being attached to a collection (and, hence,
never rendered) They are separate objects (despite having the same id) and
only one is in the controls collection.

As for just setting it, you have a similar issue. When you say newTable =
obj.GetTable(), think about what is happening here. You have a Table object
instance, which is added to the controls collection of the page by the
derived class so it can be rendered. You then take the local variable that
holds a reference to it, and point it at another Table object. This new
object, however, is never added to a collection. The original object still
sits in the controls collection of the page, you just no longer have a quick
reference to it. When you go to render the page, however, it doesn't care
what you have a reference to in your code - it just goes through the
controls collection (and sub collections, and so forth) of your page (and of
each control that has a controls collection as well) asking them to render
themselves. Since your new table is not a part of that collection, it is
summarily ignored.

If you wanted to use brute force, you could iterate through the controls
collection, find the existing table, remove it, and then add your new table.
It is, however, far easier to just put a placeholder there and add the
control in.
 
Back
Top