Accessing Properties of Custom Controls child Controls

Discussion in 'ASP .Net Web Controls' started by Moldy, Sep 4, 2003.

  1. Moldy

    Moldy Guest

    I am using a Custom Control on a page which renders a button control
    if required. I need to access the child button control's properties
    (i.e. UniqueID) on the page but cannot get them to come through on
    first load of the page, only on postback. Can anyone help!

    Code is below

    On first load, only the button is displayed, click the button and the
    UniqueID appears.

    TIA

    Moldy

    ********** START CODE FOR CUSTOM CONTROL **********
    Imports System.ComponentModel
    Imports System.Web.UI
    Imports System.Web.UI.WebControls

    Public Class ButtonControl
    Inherits System.Web.UI.WebControls.WebControl
    Implements INamingContainer

    Dim bMyButton As Boolean

    <Browsable(True), DefaultValue(False)> Public Property
    ShowMyButton() As Boolean
    Get
    Return bMyButton
    End Get
    Set(ByVal Value As Boolean)
    bMyButton = Value
    End Set
    End Property

    Public myButton As New Button()

    Protected Overrides Sub CreateChildControls()
    With myButton
    .Text = "Click Me!"
    .Visible = False
    End With
    Controls.Add(myButton)
    End Sub

    Private Sub ButtonControl_PreRender(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles MyBase.PreRender
    If bMyButton Then
    myButton.Visible = True
    Else
    myButton.Visible = False
    End If
    End Sub
    End Class
    ********** END CODE FOR CUSTOM CONTROL **********

    ********** START CODE FOR ASPX PAGE **********
    <%@ Register TagPrefix="CompControl" Namespace="CompControl"
    Assembly="CompControl" %>
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="Default.aspx.vb" Inherits="ChildControlTest.WebForm1"%>
    <html>
    <body>
    <form id="Form1" method="post" runat="server">
    <compcontrol:buttoncontrol
    id="myButtonControl" runat="server"></compcontrol:buttoncontrol>
    </form>
    </body>
    </html>
    ********** END CODE FOR ASPX PAGE **********

    ********** START CODE FOR ASPX.VB PAGE **********
    Public Class WebForm1
    Inherits System.Web.UI.Page
    Protected WithEvents myButtonControl As
    CompControl.ButtonControl

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles MyBase.Load
    myButtonControl.ShowMyButton = True
    Response.Write(myButtonControl.myButton.UniqueID)
    End Sub

    End Class
    ********** END CODE FOR ASPX.VB PAGE **********
    Moldy, Sep 4, 2003
    #1
    1. Advertising

  2. Moldy

    Moldy Guest

    On Thu, 4 Sep 2003 11:02:30 -0400, "John Saunders"
    <> wrote:

    >You need to call EnsureChildControls before referencing any child control.


    Where in the code should I place this?

    TIA

    Moldy
    Moldy, Sep 4, 2003
    #2
    1. Advertising

  3. Moldy

    Teemu Keiski Guest

    Just call it every time before you'd need to access child controls (member
    that references child controls etc). Calling EnsureChildControls makes sure
    child controls (created in CreateChildControls) are created. One example is
    calling it in accessor for top-level property that returns some proeprty
    value from the child control(s).

    --
    Teemu Keiski
    MCP, Designer/Developer
    Mansoft tietotekniikka Oy
    http://www.mansoft.fi

    AspInsiders Member, www.aspinsiders.com
    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com


    "Moldy" <> wrote in message
    news:...
    > On Thu, 4 Sep 2003 11:02:30 -0400, "John Saunders"
    > <> wrote:
    >
    > >You need to call EnsureChildControls before referencing any child

    control.
    >
    > Where in the code should I place this?
    >
    > TIA
    >
    > Moldy
    Teemu Keiski, Sep 4, 2003
    #3
  4. [snip]

    > Public myButton As New Button()


    You should declare this as a read-only property and be sure to call
    EnsureChildControls() before accessing it:

    Dim _MyButton As Boolean
    Public Property MyButton() As Boolean
    Get
    EnsureChildControls()
    Return _MyButton
    End Get
    End Property

    > Protected Overrides Sub CreateChildControls()
    > With myButton
    > .Text = "Click Me!"
    > .Visible = False
    > End With
    > Controls.Add(myButton)
    > End Sub


    This is where you should *create* the child controls, not initialize them:

    Protected Overrides Sub CreateChildControls()
    _MyButton = New Button()
    With _MyButton
    .Text = "Click Me!"
    .Visible = False
    End With
    Controls.Add(_MyButton)
    End Sub

    > Private Sub ButtonControl_PreRender(ByVal sender As Object,
    > ByVal e As System.EventArgs) Handles MyBase.PreRender
    > If bMyButton Then
    > myButton.Visible = True
    > Else
    > myButton.Visible = False
    > End If
    > End Sub


    Why use an internal variable? Why not just read/write directly to
    MyButton.Visible for the ShowMyButton property?

    Example:

    <Browsable(True), DefaultValue(False)>
    Public Property ShowMyButton() As Boolean
    Get
    Return MyButton.Visible
    End Get
    Set(ByVal Value As Boolean)
    MyButton.Visible = Value
    End Set
    End Property

    You could then exclude the step of assigning _MyButton.Visible in your
    CreateChildControls method:

    Protected Overrides Sub CreateChildControls()
    _MyButton = New Button()
    With _MyButton
    .Text = "Click Me!"
    End With
    Controls.Add(_MyButton)
    End Sub

    --
    -Jimmy
    Used-Disks:
    http://www.used-disks.com/
    Jimmy [Used-Disks], Sep 4, 2003
    #4
  5. Moldy

    Moldy Guest

    On Thu, 4 Sep 2003 18:03:10 -0400, "Jimmy [Used-Disks]"
    <> wrote:

    >[snip]
    >

    [snip]

    Jimmy, you are a *STAR* !

    Thanks muchly

    Moldy
    Moldy, Sep 5, 2003
    #5
  6. Moldy

    Moldy Guest

    On Thu, 4 Sep 2003 18:03:10 -0400, "Jimmy [Used-Disks]"
    <> wrote:

    >[snip]
    >

    [Snip]

    OK, so I got that working now except one problem.

    All the buttons in my object default to being on even tho I have set
    DefaultValue(False) on the Property.

    Why is this? The actual control I am working on has 28 buttons and I
    don't really want to have to switch them all off each time it is
    called (which is a lot!)

    Any ideas?

    New code is below....

    TIA

    Moldy

    ******CODE******

    Imports System.ComponentModel
    Imports System.Web.UI
    Imports System.Web.UI.WebControls

    Public Class ButtonControl
    Inherits System.Web.UI.WebControls.WebControl
    Implements INamingContainer

    Dim _myButton As Button
    Public ReadOnly Property MyButton() As Button
    Get
    ensurechildcontrols()
    Return _myButton
    End Get
    End Property

    <Browsable(True), DefaultValue(False)> Public Property
    ShowMyButton() As Boolean
    Get
    Return MyButton.Visible
    End Get
    Set(ByVal Value As Boolean)
    MyButton.Visible = Value
    End Set
    End Property

    Protected Overrides Sub CreateChildControls()
    _myButton = New Button()
    With _myButton
    .Text = "Click Me!"
    End With
    Controls.Add(_myButton)
    End Sub

    End Class
    Moldy, Sep 5, 2003
    #6
  7. "Moldy" <> wrote in message
    news:...
    > On Thu, 4 Sep 2003 18:03:10 -0400, "Jimmy [Used-Disks]"
    > <> wrote:
    >
    > >[snip]
    > >

    > [Snip]
    >
    > OK, so I got that working now except one problem.
    >
    > All the buttons in my object default to being on even tho I have set
    > DefaultValue(False) on the Property.
    >
    > Why is this? The actual control I am working on has 28 buttons and I
    > don't really want to have to switch them all off each time it is
    > called (which is a lot!)
    >
    > Any ideas?
    >
    > New code is below....
    >
    > TIA
    >
    > Moldy
    >
    > ******CODE******
    >
    > Imports System.ComponentModel
    > Imports System.Web.UI
    > Imports System.Web.UI.WebControls
    >
    > Public Class ButtonControl
    > Inherits System.Web.UI.WebControls.WebControl
    > Implements INamingContainer
    >
    > Dim _myButton As Button
    > Public ReadOnly Property MyButton() As Button
    > Get
    > ensurechildcontrols()
    > Return _myButton
    > End Get
    > End Property
    >
    > <Browsable(True), DefaultValue(False)> Public Property
    > ShowMyButton() As Boolean
    > Get
    > Return MyButton.Visible
    > End Get
    > Set(ByVal Value As Boolean)
    > MyButton.Visible = Value
    > End Set
    > End Property
    >
    > Protected Overrides Sub CreateChildControls()
    > _myButton = New Button()
    > With _myButton
    > .Text = "Click Me!"
    > End With
    > Controls.Add(_myButton)
    > End Sub
    >
    > End Class
    >


    When you created your button in CreateChildControls, you didn't touch the
    Visible property - it defaults to true.
    --
    John Saunders
    Internet Engineer
    John Saunders, Sep 5, 2003
    #7
  8. Hey Moldy, I'm glad I could help :).

    > Protected Overrides Sub CreateChildControls()
    > _myButton = New Button()
    > With _myButton
    > .Text = "Click Me!"


    Add this:

    .Visible = False

    > End With
    > Controls.Add(_myButton)
    > End Sub


    The reason, as John pointed out, is that the Button's visible property
    defaults to true and that is your control to ignore your default attribute.
    By setting the .Visible property on the buttons creation, you are explicitly
    turing this off.

    --
    -Jimmy Tharpe
    http://www.aps-soft.com
    Jimmy [Used-Disks], Sep 5, 2003
    #8
  9. Moldy

    Moldy Guest

    On Fri, 5 Sep 2003 11:11:27 -0400, "Jimmy [Used-Disks]"
    <> wrote:

    >Hey Moldy, I'm glad I could help :).
    >
    >> Protected Overrides Sub CreateChildControls()
    >> _myButton = New Button()
    >> With _myButton
    >> .Text = "Click Me!"

    >
    >Add this:
    >
    > .Visible = False
    >
    >> End With
    >> Controls.Add(_myButton)
    >> End Sub

    >
    >The reason, as John pointed out, is that the Button's visible property
    >defaults to true and that is your control to ignore your default attribute.
    >By setting the .Visible property on the buttons creation, you are explicitly
    >turing this off.


    Great stuff!

    I did have that in my original code but took it out as you pointed out
    that it can be set directly from the Show property. I thought that it
    would take the default from the property.

    Thanks for all your help. My app is much leaner now!

    Regards,

    Moldy
    Moldy, Sep 5, 2003
    #9
    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,080
    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:
    210
    Moldy
    Sep 5, 2003
  3. Replies:
    0
    Views:
    192
  4. Paul Yanzick
    Replies:
    4
    Views:
    489
    Steven Cheng[MSFT]
    Jan 9, 2006
  5. Donal McWeeney

    Mixing child properties and child controls - how?

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

Share This Page