Accessing Child Controls

  • Thread starter Thread starter Jonathan Williams
  • Start date Start date
J

Jonathan Williams

Hi,
I have an object which inherits from WebControl (CUSTOM : WebControl)

In this object I have code in which I add child contols:
protected override void CreateChildControls()
{
//this.Controls.Clear();
CustomFieldUtil objCustomFieldUtil = new CustomFieldUtil();
DataTable objDT = objCustomFieldUtil.GetDataTable("custom");
for(intCount = 0; intCount < objDT.Rows.Count; intCount++)
{
if (objDT.Rows[intCount]["Name"].ToString().Trim() != "")
{
LBL myLbl = new LBL();
myLbl.ID = "Label" + intCount.ToString();
myLbl.Text = objDT.Rows[intCount]["Name"].ToString();
this.Controls.Add(new LiteralControl("<tr><td class=\"regLabelCell\"
width=\"20%\">"));
this.Controls.Add(myLbl);
this.Controls.Add(new LiteralControl("</td>"));
}
....etc.
}


I'm having problems accessing those controls that I add there from another
function. When I do the following code intCount returns 0!

foreach (Control ctrl in this.Controls)
{
this.intCount++;
}

AHHH! what happend to the reference?

Thanks

-Jon
 
Sometimes kids are like that. Especially if you were too liberal in raising
them.
 
Actually I didn't define the structrue of the LBL's etc. I'm generating
based on someone else class.

-Jon

Jonathan Williams said:
You'll notice that those labels I add through create child controls are
inherit from controls themselves. LBL myLbl = new LBL(); LBL inherits from
Label

so myLbl has a couple properties and methods

Ok, here's what I'm trying to do and having trouble with.
From within CUSTOM : WebControl, INamingContainer

I want to create another function like editable in which I would access
those dynamically created LBL's and mess with thier properties
(functionality to make it editable or not is already encapulated, I just
need to set a bool to true or false)

Whey I try to access these it seems they are Literal controls or somthing.

Thanks for the help, if you can even understand me :)

-J

Steve C. Orr said:
I'm not clear where your for loop is exactly.
Is this happening outside of the control?
If so, you should try changing your code to this:

foreach (Control ctrl in myCustomControl.Controls)
{
this.intCount++;
}

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
Developer for Hire



Jonathan Williams said:
Hi,
I have an object which inherits from WebControl (CUSTOM : WebControl)

In this object I have code in which I add child contols:
protected override void CreateChildControls()
{
//this.Controls.Clear();
CustomFieldUtil objCustomFieldUtil = new CustomFieldUtil();
DataTable objDT = objCustomFieldUtil.GetDataTable("custom");
for(intCount = 0; intCount < objDT.Rows.Count; intCount++)
{
if (objDT.Rows[intCount]["Name"].ToString().Trim() != "")
{
LBL myLbl = new LBL();
myLbl.ID = "Label" + intCount.ToString();
myLbl.Text = objDT.Rows[intCount]["Name"].ToString();
this.Controls.Add(new LiteralControl("<tr><td class=\"regLabelCell\"
width=\"20%\">"));
this.Controls.Add(myLbl);
this.Controls.Add(new LiteralControl("</td>"));
}
...etc.
}


I'm having problems accessing those controls that I add there from another
function. When I do the following code intCount returns 0!

foreach (Control ctrl in this.Controls)
{
this.intCount++;
}

AHHH! what happend to the reference?

Thanks

-Jon
 
Should I be adding each of those LBL's I create into an array or collection
for future access?

-Jon


Steve C. Orr said:
I'm not clear where your for loop is exactly.
Is this happening outside of the control?
If so, you should try changing your code to this:

foreach (Control ctrl in myCustomControl.Controls)
{
this.intCount++;
}

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
Developer for Hire



Jonathan Williams said:
Hi,
I have an object which inherits from WebControl (CUSTOM : WebControl)

In this object I have code in which I add child contols:
protected override void CreateChildControls()
{
//this.Controls.Clear();
CustomFieldUtil objCustomFieldUtil = new CustomFieldUtil();
DataTable objDT = objCustomFieldUtil.GetDataTable("custom");
for(intCount = 0; intCount < objDT.Rows.Count; intCount++)
{
if (objDT.Rows[intCount]["Name"].ToString().Trim() != "")
{
LBL myLbl = new LBL();
myLbl.ID = "Label" + intCount.ToString();
myLbl.Text = objDT.Rows[intCount]["Name"].ToString();
this.Controls.Add(new LiteralControl("<tr><td class=\"regLabelCell\"
width=\"20%\">"));
this.Controls.Add(myLbl);
this.Controls.Add(new LiteralControl("</td>"));
}
...etc.
}


I'm having problems accessing those controls that I add there from another
function. When I do the following code intCount returns 0!

foreach (Control ctrl in this.Controls)
{
this.intCount++;
}

AHHH! what happend to the reference?

Thanks

-Jon
 
That sounds like a good thing to try.

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
Developer for Hire


Jonathan Williams said:
Should I be adding each of those LBL's I create into an array or collection
for future access?

-Jon


Steve C. Orr said:
I'm not clear where your for loop is exactly.
Is this happening outside of the control?
If so, you should try changing your code to this:

foreach (Control ctrl in myCustomControl.Controls)
{
this.intCount++;
}

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
Developer for Hire



Jonathan Williams said:
Hi,
I have an object which inherits from WebControl (CUSTOM : WebControl)

In this object I have code in which I add child contols:
protected override void CreateChildControls()
{
//this.Controls.Clear();
CustomFieldUtil objCustomFieldUtil = new CustomFieldUtil();
DataTable objDT = objCustomFieldUtil.GetDataTable("custom");
for(intCount = 0; intCount < objDT.Rows.Count; intCount++)
{
if (objDT.Rows[intCount]["Name"].ToString().Trim() != "")
{
LBL myLbl = new LBL();
myLbl.ID = "Label" + intCount.ToString();
myLbl.Text = objDT.Rows[intCount]["Name"].ToString();
this.Controls.Add(new LiteralControl("<tr><td class=\"regLabelCell\"
width=\"20%\">"));
this.Controls.Add(myLbl);
this.Controls.Add(new LiteralControl("</td>"));
}
...etc.
}


I'm having problems accessing those controls that I add there from another
function. When I do the following code intCount returns 0!

foreach (Control ctrl in this.Controls)
{
this.intCount++;
}

AHHH! what happend to the reference?

Thanks

-Jon
 
This whole thread is me talking to myself LOL ;-)

So what I did was renamed the protected override void CreateChildControls()
to private void myCreateChildControls() and everything started working like
I wanted it to. That is the method calls to the LBL's etc.

I guess I just don't understand the purpose of the protected override void
CreateChildControls() and was using it incorrectly.


Jonathan Williams said:
Should I be adding each of those LBL's I create into an array or collection
for future access?

-Jon


Steve C. Orr said:
I'm not clear where your for loop is exactly.
Is this happening outside of the control?
If so, you should try changing your code to this:

foreach (Control ctrl in myCustomControl.Controls)
{
this.intCount++;
}

--
I hope this helps,
Steve C. Orr, MCSD
http://Steve.Orr.net
Developer for Hire



Jonathan Williams said:
Hi,
I have an object which inherits from WebControl (CUSTOM : WebControl)

In this object I have code in which I add child contols:
protected override void CreateChildControls()
{
//this.Controls.Clear();
CustomFieldUtil objCustomFieldUtil = new CustomFieldUtil();
DataTable objDT = objCustomFieldUtil.GetDataTable("custom");
for(intCount = 0; intCount < objDT.Rows.Count; intCount++)
{
if (objDT.Rows[intCount]["Name"].ToString().Trim() != "")
{
LBL myLbl = new LBL();
myLbl.ID = "Label" + intCount.ToString();
myLbl.Text = objDT.Rows[intCount]["Name"].ToString();
this.Controls.Add(new LiteralControl("<tr><td class=\"regLabelCell\"
width=\"20%\">"));
this.Controls.Add(myLbl);
this.Controls.Add(new LiteralControl("</td>"));
}
...etc.
}


I'm having problems accessing those controls that I add there from another
function. When I do the following code intCount returns 0!

foreach (Control ctrl in this.Controls)
{
this.intCount++;
}

AHHH! what happend to the reference?

Thanks

-Jon
 
Back
Top