Please Help! Dynamic User Controls

Discussion in 'ASP .Net Web Controls' started by NKaufman, Aug 15, 2007.

  1. NKaufman

    NKaufman Guest

    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..
     
    NKaufman, Aug 15, 2007
    #1
    1. Advertising

  2. NKaufman

    NKaufman Guest

    Re: Please Help! Dynamic User Controls

    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);
     
    NKaufman, Aug 15, 2007
    #2
    1. Advertising

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

    --
    Jesse Houwing
    jesse.houwing at sogeti.nl
     
    Jesse Houwing, Aug 16, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    5,599
  2. Ronald
    Replies:
    2
    Views:
    860
    Robbe Morris [C# MVP]
    Jan 17, 2006
  3. KK
    Replies:
    2
    Views:
    648
    Big Brian
    Oct 14, 2003
  4. NKaufman

    Please Help! Dynamic User Controls

    NKaufman, Aug 15, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    311
    NKaufman
    Aug 15, 2007
  5. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    484
    Eidolon
    May 4, 2004
Loading...

Share This Page