DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Wed, 15 Aug 2007 14:37:27 -0700,    posted on: microsoft.public.dotnet.framework.aspnet.webcontrols        back       

Thread Index
  1    NKaufman
          2    NKaufman
          3    Jesse Houwing am
          4    Jesse Houwing am
          5    Jesse Houwing am


Please Help! Dynamic User Controls   
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..
Date:Wed, 15 Aug 2007 14:37:27 -0700   Author:  

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);
Date:Wed, 15 Aug 2007 15:04:05 -0700   Author:  

Re: Please Help! Dynamic User Controls   
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
Date:Thu, 16 Aug 2007 11:05:32 +0000 (UTC)   Author:  

Re: Please Help! Dynamic User Controls   
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
Date:Thu, 16 Aug 2007 11:05:32 +0000 (UTC)   Author:  

Re: Please Help! Dynamic User Controls   
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
Date:Thu, 16 Aug 2007 11:05:32 +0000 (UTC)   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us