Accessing Child Controls

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
 
G

Guenther Liebowitz

Sometimes kids are like that. Especially if you were too liberal in raising
them.
 
J

Jonathan Williams

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
 
J

Jonathan Williams

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
 
S

Steve C. Orr, MCSD

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
 
J

Jonathan Williams

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
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top