how do child controls obtain their IDs?

J

Jiho Han

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.
 
A

Alessandro Zifiglio

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.

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 :
button1.id = "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 :

textbox1 = new textbox
validator1 = new RequiredFieldValidator
controls.add(textbox1)
controls.add(validator1)
textbox1.id = "textbox1"
validator1.ControlToValidate = textbox1.id '<<<===== textbox1.id contains "textbox1"
validator1.ErrorMessage = "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:

<input name="uniqeControlName1:textbox1" type="text" value="" id="uniqueControlName1_textbox1" />
<span id="uniqueControlName1__ctl3" controltovalidate="uniqueControlName1_textbox1" errormessage="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 :
<input name="uniqueControlName1:_ctl3" type="text" value="" />

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 :

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

Dim textbox2 As TextBox
textbox2 = New TextBox()

textbox2 = 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 :
we have been able to retrieve textbox1 from our controls collection : textbox1

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

Alessandro Zifiglio

oops, typo error where i was using response.write to write out textbox2.id, i wrote out textbox1.id --
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.

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 :
button1.id = "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 :

textbox1 = new textbox
validator1 = new RequiredFieldValidator
controls.add(textbox1)
controls.add(validator1)
textbox1.id = "textbox1"
validator1.ControlToValidate = textbox1.id '<<<===== textbox1.id contains "textbox1"
validator1.ErrorMessage = "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:

<input name="uniqeControlName1:textbox1" type="text" value="" id="uniqueControlName1_textbox1" />
<span id="uniqueControlName1__ctl3" controltovalidate="uniqueControlName1_textbox1" errormessage="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 :
<input name="uniqueControlName1:_ctl3" type="text" value="" />

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 :

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

Dim textbox2 As TextBox
textbox2 = New TextBox()

textbox2 = 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 :
we have been able to retrieve textbox1 from our controls collection : textbox1

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top