Please Help! Dynamic User Controls

N

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

NKaufman

Here is my PageLoad snippet:


HtmlTable itemTable2 = new HtmlTable();
itemTable2.Width = "1200";
HtmlTableRow itemRow2 = new HtmlTableRow();
int j = 0;

foreach (DataRow myrow in ds.Tables["Sub"].Rows)
{
HtmlTableCell itemCell1 = new HtmlTableCell();
itemCell1.Width = "200";
Control MyControl = LoadControl("Control1.ascx");
Control1 MyC = (Control1)MyControl;
MyC.MyID = Convert.ToInt32(myrow["ID"]);
MyC.MyParentID = Convert.ToInt32(myrow["ParentID"]);
MyC.ID = "Control3" + j;
MyC.MyDataRow = myrow;

MyC.BubbleClick += new
EventHandler(Default3_BubbleClick);

itemCell1.Controls.Add(MyC);
itemRow2.Cells.Add(itemCell1);

HtmlTableCell itemCell2 = new HtmlTableCell();
itemCell2.Width = "20";
itemCell2.InnerHtml=" ";
itemRow2.Cells.Add(itemCell2);
j++;
}
itemTable2.Controls.Add(itemRow2);
Panel3.Controls.Add(itemTable2);
 
J

Jesse Houwing

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top