Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Accessing Properties of Custom Controls child Controls

Reply
Thread Tools

Accessing Properties of Custom Controls child Controls

 
 
Moldy
Guest
Posts: n/a
 
      09-04-2003
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 **********
 
Reply With Quote
 
 
 
 
Moldy
Guest
Posts: n/a
 
      09-04-2003
On Thu, 4 Sep 2003 11:02:30 -0400, "John Saunders"
<(E-Mail Removed)> wrote:

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


Where in the code should I place this?

TIA

Moldy
 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      09-04-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 4 Sep 2003 11:02:30 -0400, "John Saunders"
> <(E-Mail Removed)> wrote:
>
> >You need to call EnsureChildControls before referencing any child

control.
>
> Where in the code should I place this?
>
> TIA
>
> Moldy



 
Reply With Quote
 
Jimmy [Used-Disks]
Guest
Posts: n/a
 
      09-04-2003
[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/


 
Reply With Quote
 
Moldy
Guest
Posts: n/a
 
      09-05-2003
On Thu, 4 Sep 2003 18:03:10 -0400, "Jimmy [Used-Disks]"
<(E-Mail Removed)> wrote:

>[snip]
>

[snip]

Jimmy, you are a *STAR* !

Thanks muchly

Moldy

 
Reply With Quote
 
Moldy
Guest
Posts: n/a
 
      09-05-2003
On Thu, 4 Sep 2003 18:03:10 -0400, "Jimmy [Used-Disks]"
<(E-Mail Removed)> 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

 
Reply With Quote
 
John Saunders
Guest
Posts: n/a
 
      09-05-2003
"Moldy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Thu, 4 Sep 2003 18:03:10 -0400, "Jimmy [Used-Disks]"
> <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Jimmy [Used-Disks]
Guest
Posts: n/a
 
      09-05-2003
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


 
Reply With Quote
 
Moldy
Guest
Posts: n/a
 
      09-05-2003
On Fri, 5 Sep 2003 11:11:27 -0400, "Jimmy [Used-Disks]"
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Composite Control and accessing complex properties of child controls - C# VS2005 Paul Yanzick ASP .Net Building Controls 4 01-09-2006 01:11 AM
HELP! Child controls of child controls not visible to web app lisa@starways.net ASP .Net Building Controls 0 05-05-2005 09:14 PM
Mixing child properties and child controls - how? Donal McWeeney ASP .Net Web Controls 3 02-14-2005 02:51 AM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM
Accessing Properties of Custom Controls child Controls Moldy ASP .Net Building Controls 8 09-05-2003 03:39 PM



Advertisments