all groups > asp.net building controls > january 2004 >
You're in the

asp.net building controls

group:

how do child controls obtain their IDs?


how do child controls obtain their IDs? Jiho Han
1/30/2004 10:06:00 AM
asp.net building controls:
I have a composite control which dynamically adds child controls to the
Controls collection in CreateChildControls method, based on various
properties that are set by the page developer.

In any case, my question is at what point do child controls obtain their
IDs? Or do they at all or do I have to assign them manually? Surely by
rendering phase, they have to have IDs available - if they are automatically
set at all.

Another question is that when you drop a control onto the designer, vs.net
automatically IDs the control? I am guessing then that's a VS.NET thing and
not the page control?

Thanks in advance.

Re: how do child controls obtain their IDs? Alessandro Zifiglio
1/30/2004 5:55:48 PM
hi Jiho, ironically it so happens that I was experimenting with this =
today, also because I was having problems supplying ID's to child =
controls when having my control implement INamingContainer.=20

The answer is to implement this interface. When this interface is =
implemented a unique id is supplied for databound items, whereas a =
default name is supplied(*note: not id) to all other controls, this =
happens for all controls you do not explicitly provide an id value. =
However if you needed to later on retrieve the id of your control then =
you need to explicitly supply an id for these child controls in the form =
:=20
button1.id =3D "button1" and this interface will see to it that your =
control has a unique id when rendered, that is : =
"uniquecontrolName_button1".

Your question is when is the id available to you.. .. .from the code =
below you can see that this is available to you immediately after you =
supply an id to a child control, that is if in your CreateChildControls =
method you were adding the following controls and needed to pass the ID =
of your childcontrol to a validator control :=20

textbox1 =3D new textbox
validator1 =3D new RequiredFieldValidator
controls.add(textbox1)
controls.add(validator1)
textbox1.id =3D "textbox1"
validator1.ControlToValidate =3D textbox1.id '<<<=3D=3D=3D=3D=3D =
textbox1.id contains "textbox1"
validator1.ErrorMessage =3D "Text is required"


As you can see the id of textbox1 is passed to the validator within the =
same method. The unique name wrapper is supplied when the control is =
rendered, that is "uniqueControlName_textbox1".

When this is rendered to screen:=20

<input name=3D"uniqeControlName1:textbox1" type=3D"text" value=3D"" =
id=3D"uniqueControlName1_textbox1" />
<span id=3D"uniqueControlName1__ctl3" =
controltovalidate=3D"uniqueControlName1_textbox1" errormessage=3D"Text =
is required" .... ></span>

if you had never supplied an id for textbox1 then no id will be supplied =
but a name is supplied like :=20
<input name=3D"uniqueControlName1:_ctl3" type=3D"text" value=3D"" />

I have further noticed that a default unique id is supplied for non =
databound items like our validator control in the example has a unique =
id. I guess this is because validator controls have clientside =
validation behaviour, and an explict id is required for these, so if you =
didnt supply an id a unique id is supplied by default for these.

I dug a little futher by testing to see if find control could actually =
find the control from within our controls collection when we supply our =
id "textbox1" to it whereas to what is actually rendered to screen, that =
is : "uniqueControlName1_textbox1"

again from within the createChildControls method :=20

textbox1 =3D new textbox
validator1 =3D new RequiredFieldValidator
controls.add(textbox1)
controls.add(validator1)
textbox1.id =3D "textbox1"
validator1.ControlToValidate =3D textbox1.id '<<<=3D=3D=3D=3D=3D =
textbox1.id contains "textbox1"
validator1.ErrorMessage =3D "Text is required"

Dim textbox2 As TextBox
textbox2 =3D New TextBox()

textbox2 =3D CType(Me.FindControl(textbox1.ID), TextBox)
context.Response.Write("we have been able to retrieve =
textbox1 from our controls collection : " & textbox1.ID)


and this worked perfectly well with the FindControl method. the output =
to screen was :=20
we have been able to retrieve textbox1 from our controls collection : =
textbox1=20

Hope this got helpful to you as it were for me.

[quoted text, click to view]
Re: how do child controls obtain their IDs? Alessandro Zifiglio
1/30/2004 6:27:05 PM
oops, typo error where i was using response.write to write out =
textbox2.id, i wrote out textbox1.id --
"Alessandro Zifiglio" <alessandrozifiglio@NO-SPAM-hotmail.com> wrote =
in message news:I5wSb.1645$HO2.1139@news.edisontel.com...
hi Jiho, ironically it so happens that I was experimenting with this =
today, also because I was having problems supplying ID's to child =
controls when having my control implement INamingContainer.=20

The answer is to implement this interface. When this interface is =
implemented a unique id is supplied for databound items, whereas a =
default name is supplied(*note: not id) to all other controls, this =
happens for all controls you do not explicitly provide an id value. =
However if you needed to later on retrieve the id of your control then =
you need to explicitly supply an id for these child controls in the form =
:=20
button1.id =3D "button1" and this interface will see to it that your =
control has a unique id when rendered, that is : =
"uniquecontrolName_button1".

Your question is when is the id available to you.. .. .from the code =
below you can see that this is available to you immediately after you =
supply an id to a child control, that is if in your CreateChildControls =
method you were adding the following controls and needed to pass the ID =
of your childcontrol to a validator control :=20

textbox1 =3D new textbox
validator1 =3D new RequiredFieldValidator
controls.add(textbox1)
controls.add(validator1)
textbox1.id =3D "textbox1"
validator1.ControlToValidate =3D textbox1.id '<<<=3D=3D=3D=3D=3D =
textbox1.id contains "textbox1"
validator1.ErrorMessage =3D "Text is required"


As you can see the id of textbox1 is passed to the validator within =
the same method. The unique name wrapper is supplied when the control is =
rendered, that is "uniqueControlName_textbox1".

When this is rendered to screen:=20

<input name=3D"uniqeControlName1:textbox1" type=3D"text" value=3D"" =
id=3D"uniqueControlName1_textbox1" />
<span id=3D"uniqueControlName1__ctl3" =
controltovalidate=3D"uniqueControlName1_textbox1" errormessage=3D"Text =
is required" .... ></span>

if you had never supplied an id for textbox1 then no id will be =
supplied but a name is supplied like :=20
<input name=3D"uniqueControlName1:_ctl3" type=3D"text" value=3D"" />

I have further noticed that a default unique id is supplied for non =
databound items like our validator control in the example has a unique =
id. I guess this is because validator controls have clientside =
validation behaviour, and an explict id is required for these, so if you =
didnt supply an id a unique id is supplied by default for these.

I dug a little futher by testing to see if find control could actually =
find the control from within our controls collection when we supply our =
id "textbox1" to it whereas to what is actually rendered to screen, that =
is : "uniqueControlName1_textbox1"

again from within the createChildControls method :=20

textbox1 =3D new textbox
validator1 =3D new RequiredFieldValidator
controls.add(textbox1)
controls.add(validator1)
textbox1.id =3D "textbox1"
validator1.ControlToValidate =3D textbox1.id '<<<=3D=3D=3D=3D=3D =
textbox1.id contains "textbox1"
validator1.ErrorMessage =3D "Text is required"

Dim textbox2 As TextBox
textbox2 =3D New TextBox()

textbox2 =3D CType(Me.FindControl(textbox1.ID), TextBox)
context.Response.Write("we have been able to retrieve =
textbox1 from our controls collection : " & textbox1.ID)


and this worked perfectly well with the FindControl method. the output =
to screen was :=20
we have been able to retrieve textbox1 from our controls collection : =
textbox1=20

Hope this got helpful to you as it were for me.

[quoted text, click to view]
AddThis Social Bookmark Button