473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1965
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sck10 | last post by:
Hello, I am looping through each control using the following to find a particular control (strFindCtl = "tbxCompetitor" & strForLoop1 & "Product"). For ctrRow = 1 To 3 strForLoop1 = "0" &...
1
by: Jaime Stuardo | last post by:
Hi all.... I am programming in ASP.NET 2.0 and VS.NET 2005 Beta 2. I have a MasterPage that provides basic look & feel for my pages. The main form present in the master page has this...
3
by: Ron | last post by:
Hi all, I've come across a problem when casting user controls in ASP.NET 2.0, hopefully someone can help. I have a master page with a user control on it (myMenu) that I wish to programmatically...
2
by: Scott | last post by:
I'm using a master page (mp_TableMaint.aspx) with one Content area (ID: Content1). Content1 contains several usercontrols, which are hidden. The goal is to show the correct control based on the...
0
by: Adam Tibi | last post by:
Hello, I have this: MasterPage 1: <asp:ContentPlaceHolder ID="cphHead" runat="server"> </asp:ContentPlaceHolder> Some Html... <asp:ContentPlaceHolder ID="cphBody1" runat="server">...
1
by: dawg1998 | last post by:
I have a page that creates dynamic textboxes based on the number of fields a user chooses to fill out. Basically, I am able to create the controls via this method (with other details): Dim...
1
by: RicercatoreSbadato | last post by:
hi all i'm using asp.net 2 and the master page paradigm in another page .aspx - that inherit from the master page - i'd like to has - programmatically - all the controls in the page, how can I...
2
by: =?Utf-8?B?a2V2aW4=?= | last post by:
using VS2005 My masterpage has two panels and two hyperlinks. The initial content aspx page is blank. The page_load event of the masterpage hides one of the panels based on SESSION...
10
by: Cirene | last post by:
I know that sometimes referring to controls deep in a page using a Master Page can be funky. For a website using a standard header/footer/nav, do you prefer using User Controls for the common...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.