Accessing Child Controls

Discussion in 'ASP .Net' started by Jonathan Williams, Aug 14, 2003.

  1. 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
     
    Jonathan Williams, Aug 14, 2003
    #1
    1. Advertising

  2. Sometimes kids are like that. Especially if you were too liberal in raising
    them.
     
    Guenther Liebowitz, Aug 14, 2003
    #2
    1. Advertising

  3. Actually I didn't define the structrue of the LBL's etc. I'm generating
    based on someone else class.

    -Jon

    "Jonathan Williams" <> wrote in message
    news:bhgq69$4o5$...
    > 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:bhejss$qqh$...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
     
    Jonathan Williams, Aug 14, 2003
    #3
  4. Should I be adding each of those LBL's I create into an array or collection
    for future access?

    -Jon


    "Steve C. Orr, MCSD" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:bhejss$qqh$...
    > > 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
    > >
    > >

    >
    >
     
    Jonathan Williams, Aug 14, 2003
    #4
  5. 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" <> wrote in message
    news:bhh1gj$dju$...
    > Should I be adding each of those LBL's I create into an array or

    collection
    > for future access?
    >
    > -Jon
    >
    >
    > "Steve C. Orr, MCSD" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:bhejss$qqh$...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
     
    Steve C. Orr, MCSD, Aug 14, 2003
    #5
  6. 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" <> wrote in message
    news:bhh1gj$dju$...
    > Should I be adding each of those LBL's I create into an array or

    collection
    > for future access?
    >
    > -Jon
    >
    >
    > "Steve C. Orr, MCSD" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > news:bhejss$qqh$...
    > > > 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
    > > >
    > > >

    > >
    > >

    >
    >
     
    Jonathan Williams, Aug 15, 2003
    #6
    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. Jeff Rodriguez
    Replies:
    23
    Views:
    1,183
    David Schwartz
    Dec 9, 2003
  2. Moldy

    Accessing Properties of Custom Controls child Controls

    Moldy, Sep 4, 2003, in forum: ASP .Net Building Controls
    Replies:
    8
    Views:
    237
    Moldy
    Sep 5, 2003
  3. Replies:
    0
    Views:
    217
  4. Moldy
    Replies:
    8
    Views:
    261
    Moldy
    Sep 5, 2003
  5. Donal McWeeney

    Mixing child properties and child controls - how?

    Donal McWeeney, Feb 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    132
    [MSFT]
    Feb 14, 2005
Loading...

Share This Page