470,641 Members | 1,736 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,641 developers. It's quick & easy.

Changed controls IDs when using a MasterPage

WT
Hello,

It seems that when a control is set into a ContentPlaceHolder in a page
using a MasterPage, it's id is change and FindControl(ID) no more works, is
it normal, how to override this or retrieve the controls OnInit handler with
a FindControl ?
Thanks again for help.
CS
Feb 21 '07 #1
7 1760
anytime a control has a new parent, its name is changed. you can only
find a control from its immediate parent unless you use recursion. you
did not give enough detail to know what you are doing wrong.

-- bruce (sqlwork.com)

WT wrote:
Hello,

It seems that when a control is set into a ContentPlaceHolder in a page
using a MasterPage, it's id is change and FindControl(ID) no more works, is
it normal, how to override this or retrieve the controls OnInit handler with
a FindControl ?
Thanks again for help.
CS

Feb 21 '07 #2
Hi there,

I think i know what his problem is. DO NOT rely on server control's ID
property neither in CSS style sheets nor javasctript - NEVER!!!. It is not
quaranteed rendered client id is the same as ID.

YOU MAY NOT DO SUCH TRICKS!!!!
<style type="text/css">
#myTextBox
{
font-size: 10pt;
}
</style>

<asp:TextBox ID="myTextBox" runat="server"/>

it will ONLY work only if text box is placed directly on page that has no
master page defined. If you use masterpage with content placeholder, or any
other parent control, id generated in HTML will include all parent controls
id.

--
Milosz
"WT" wrote:
Hello,

It seems that when a control is set into a ContentPlaceHolder in a page
using a MasterPage, it's id is change and FindControl(ID) no more works, is
it normal, how to override this or retrieve the controls OnInit handler with
a FindControl ?
Thanks again for help.
CS
Feb 21 '07 #3
Hi CS,

FindControl only finds direct child in the control container. Controls put
into a ContentPlaceHolder will need to use the ContentPlaceHolder to find
the controls since the ContentPlaceHolder is a naming container (implements
INamingContainer).

#Control.NamingContainer Property (System.Web.UI)
http://msdn2.microsoft.com/en-us/lib....namingcontain
er.aspx

#Accessing ASP.NET Controls Programmatically
http://msdn2.microsoft.com/en-us/library/ms178509.aspx

#How to find child controls that are located in the template of a parent
control
http://support.microsoft.com/kb/323261

#ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
http://www.odetocode.com/Articles/450.aspx
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 22 '07 #4
WT
Hi,

Thanks for answer, yes it is unusable with masterpages.
I was working serverside, not client, I discovered that using findcontrol
was a very complicated method as using object generated by asp.net for the
controls was working, and theses objects instances have the name of the
ID...that has changed after. So it is Ok until v3....
CS
"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.pla écrit dans le message
de news: A5**********************************@microsoft.com...
Hi there,

I think i know what his problem is. DO NOT rely on server control's ID
property neither in CSS style sheets nor javasctript - NEVER!!!. It is not
quaranteed rendered client id is the same as ID.

YOU MAY NOT DO SUCH TRICKS!!!!
<style type="text/css">
#myTextBox
{
font-size: 10pt;
}
</style>

<asp:TextBox ID="myTextBox" runat="server"/>

it will ONLY work only if text box is placed directly on page that has no
master page defined. If you use masterpage with content placeholder, or
any
other parent control, id generated in HTML will include all parent
controls
id.

--
Milosz
"WT" wrote:
>Hello,

It seems that when a control is set into a ContentPlaceHolder in a page
using a MasterPage, it's id is change and FindControl(ID) no more works,
is
it normal, how to override this or retrieve the controls OnInit handler
with
a FindControl ?
Thanks again for help.
CS

Feb 22 '07 #5
WT
Thanks one more time, very usefull.
CS
"Walter Wang [MSFT]" <wa****@online.microsoft.coma écrit dans le message
de news: kN**************@TK2MSFTNGHUB02.phx.gbl...
Hi CS,

FindControl only finds direct child in the control container. Controls put
into a ContentPlaceHolder will need to use the ContentPlaceHolder to find
the controls since the ContentPlaceHolder is a naming container
(implements
INamingContainer).

#Control.NamingContainer Property (System.Web.UI)
http://msdn2.microsoft.com/en-us/lib....namingcontain
er.aspx

#Accessing ASP.NET Controls Programmatically
http://msdn2.microsoft.com/en-us/library/ms178509.aspx

#How to find child controls that are located in the template of a parent
control
http://support.microsoft.com/kb/323261

#ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
http://www.odetocode.com/Articles/450.aspx
Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no
rights.

Feb 22 '07 #6
Hi there again,

The rule is : never use server control ID for CSS and javascript. Period. i
didn't mean master pages only, it would stop working if you place control
inside another control, gridview, panel, repeater. Even if it seemed to be
working, it's very easy to screwed it up by adding controls, and then
spending hours finding the bug :)

use css class names instead.

--
Milosz
"WT" wrote:
Hi,

Thanks for answer, yes it is unusable with masterpages.
I was working serverside, not client, I discovered that using findcontrol
was a very complicated method as using object generated by asp.net for the
controls was working, and theses objects instances have the name of the
ID...that has changed after. So it is Ok until v3....
CS
"Milosz Skalecki [MCAD]" <mi*****@DONTLIKESPAMwp.pla écrit dans le message
de news: A5**********************************@microsoft.com...
Hi there,

I think i know what his problem is. DO NOT rely on server control's ID
property neither in CSS style sheets nor javasctript - NEVER!!!. It is not
quaranteed rendered client id is the same as ID.

YOU MAY NOT DO SUCH TRICKS!!!!
<style type="text/css">
#myTextBox
{
font-size: 10pt;
}
</style>

<asp:TextBox ID="myTextBox" runat="server"/>

it will ONLY work only if text box is placed directly on page that has no
master page defined. If you use masterpage with content placeholder, or
any
other parent control, id generated in HTML will include all parent
controls
id.

--
Milosz
"WT" wrote:
Hello,

It seems that when a control is set into a ContentPlaceHolder in a page
using a MasterPage, it's id is change and FindControl(ID) no more works,
is
it normal, how to override this or retrieve the controls OnInit handler
with
a FindControl ?
Thanks again for help.
CS


Feb 22 '07 #7
Hi CS,

What Milosz described is mostly about the client-side ID (Control.ClientID:
http://msdn2.microsoft.com/en-us/lib....clientid.aspx
) generated from server-side controls. Which is a different concept (though
related) to the server-side ID (Contro.ID:
http://msdn2.microsoft.com/en-us/lib...ntrol.id.aspx).

Another "ID" is Control.UniqueID.

To differentiate these three types of ID, please refer to following related
resources:

#Client Script in ASP.NET Web Pages
http://msdn2.microsoft.com/en-us/lib...2a(VS.80).aspx

#Shane's Blog - ASP.NET: Web Controls have an ID, a ClientID and a
UniqueID. What's the deal?
http://www.shanebauer.com/Weblog/Per...400b-aacf-e94d
4da9b533.aspx

Anyway, please feel free to let me know if you still have any questions
regarding these properties and where/when to use them accordingly.

Regards,
Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Feb 23 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Scott | last post: by
reply views Thread by Adam Tibi | last post: by
1 post views Thread by dawg1998 | last post: by
1 post views Thread by RicercatoreSbadato | last post: by
2 posts views Thread by =?Utf-8?B?a2V2aW4=?= | last post: by
10 posts views Thread by Cirene | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.