By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

Accessing Child Controls

P: n/a
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
Nov 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Sometimes kids are like that. Especially if you were too liberal in raising
them.
Nov 17 '05 #2

P: n/a
Actually I didn't define the structrue of the LBL's etc. I'm generating
based on someone else class.

-Jon

"Jonathan Williams" <an**@covad.net> wrote in message
news:bh**********@sun-news.laserlink.net...
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, MCSD" <St***@Orr.net> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
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" <an**@covad.net> wrote in message
news:bh**********@sun-news.laserlink.net...
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



Nov 17 '05 #3

P: n/a
Should I be adding each of those LBL's I create into an array or collection
for future access?

-Jon
"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
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" <an**@covad.net> wrote in message
news:bh**********@sun-news.laserlink.net...
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


Nov 17 '05 #4

P: n/a
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" <an**@covad.net> wrote in message
news:bh**********@sun-news.laserlink.net...
Should I be adding each of those LBL's I create into an array or collection for future access?

-Jon
"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
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" <an**@covad.net> wrote in message
news:bh**********@sun-news.laserlink.net...
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



Nov 17 '05 #5

P: n/a
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" <an**@covad.net> wrote in message
news:bh**********@sun-news.laserlink.net...
Should I be adding each of those LBL's I create into an array or collection for future access?

-Jon
"Steve C. Orr, MCSD" <St***@Orr.net> wrote in message
news:eP**************@TK2MSFTNGP10.phx.gbl...
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" <an**@covad.net> wrote in message
news:bh**********@sun-news.laserlink.net...
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



Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.