Hello NKaufman,
In my pageload I am adding multiple instances of same User control.
They all have properties ID and ParentID.
Now I also have a button in each User control and am bubbling the
event back to main form. No problem so far.
Now in the event handler method, I get the ID of the user control that
raised the event (through the ID property of sender). I now need to go
through all the user controls on my page to see which one has parentID
matching the ID of the event raising user control.
foreach (Control1 MyCo in this.Page.Controls)
{
Response.Write(MyCo.GetType());
if (MyCo.MyParentID == ParID)
MyCo.Visible = false;
}
I do not see any user control in the output of response.write
Right now, the controls are loaded everytime the page is loaded.
Please help..
Looking at your other code you're putting the controls in a tablecelll, which
is nested in a tablerow, which is nested in a table, which is finally nested
in your page.
So the structure woudl come down to this:
this.Page
___+- Table
_____+-TableRow
_______+- TableCell
__________+- UserControl
So your loop will only find the top level controls in the Page's Controls
collection (probably a Table, a Form and a few HTMLGeneric controls).
In order to display all controls on the page you need to use a recursive
function:
public void PrintControlTree(Control baseControl)
{
foreach (Control c in baseControl.Controls)
{
Response.Write(c.GetType());
if (c.HasChildControls)
{
PrintControlTree(c);
}
}
}
You can also use Page.FindControl("MyControlName") to look them up directly.
You could also store all your usercontrols in a List<MyControl> on the page
after you've initialised them, that way you can easily find them if needed.
private List<MyControl> _myControlList = new List<MyControl>();
On another note. I see in your initialization code that you're doing a few
things that might break under certain circumstances.
when dynamically builing a control tree, always keep to this order:
- initialise the control
- set it's ID
- add it to it's container
- do other stuff with it.
This has to do with the way ViewState is loaded when controls are added to
the control tree.
So in your little example:
HtmlTable tb = new HtmlTable();
tb.ID = "Table"; // might be able to skip this call
Panel3.Controls.Add(tb);
HtmlTableRow tr = new HtmlTableRow();
tr.ID = "Row" + j; // might be able to skip this call
tb.Controls.Add(tr);
HtmlTableCell tc = new HtmlTableCell();
tc.ID = "Cell" + j // might be able to skip this call
tr.Controls.Add(tc);
MyControl mc = LoadControl("Control1.ascx") as MyControl;
if (mc != null)
{
mc.ID = "MyControl_"+j
tc.Controls.Add(mc);
mc.MyID = (int)myrow["id"];
mc.ParentID = (int)myrow["parentid"];
// if you're using the ArrayList
_myControlList.Add(mc);
}
So exactly the other way around.
One last note, please use multiposting (one newsgroup post directed to multiple
groups at one) and onyl if you have really no idea whereto put it. This is
clearly an ASP.NET issue, so there's no need to post to dotnet.general. You
aren't building your own WebControl, so you could've skipped there too.