Changed controls IDs when using a MasterPage

W

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
 
B

bruce barker

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)
 
G

Guest

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

Walter Wang [MSFT]

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/library/system.web.ui.control.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 ([email protected], 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.
 
W

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
 
G

Guest

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

Walter Wang [MSFT]

Hi CS,

What Milosz described is mostly about the client-side ID (Control.ClientID:
http://msdn2.microsoft.com/en-us/library/system.web.ui.control.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/library/system.web.ui.control.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/library/3hc29e2a(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/PermaLink,guid,488f7bb3-dac8-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 ([email protected], 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.
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top