master-pages: naming problem for client-side scripting

R

Roberto Kohler

ASP.NET renames Textboxes and other controls in aspx files that use
master-pages ?

I've discovered that aspx files that use master-pages have their Textboxes
and other controls renamed before they are rendered. For example I have an
aspx file with a textbox named "City", when it gets loaded at the client,
the textbox "txtCity" is now named "ctl00$bodyContentPlaceHolder$City".

That means that if I am writing client side scripts that need to manipulate
the Textbox "City", they need to be re-written to manipulate
"ctl00$bodyContentPlaceHolder$City" instead of "City".

Is there a way to dynamically determine the text string ASP.NET is going to
prepend to the html tag names?
Is the text string ASP.NET prepends always "ctl00$bodyContentPlaceHolder$" ?
What is the best way to handle this?
 
P

Phillip Williams

Hi Roberto,

I usually employ a JavaScript function that looks for a control of a
particular tagname that ends with the ID that I want, e.g. the following
function would get you one control at a time of type input (which is the
object rendered by a textbox):

function findControl(ControlID)
{ var ret=null;
var aControls = document.getElementsByTagName("input");
if (aControls)
{ for (var i=0; i< aControls.length ; i++)
{
if (aControls.id.lastIndexOf(ControlID) == aControls.id.length -
ControlID.length &&
aControls.id.length != ControlID.length &&
aControls.id.lastIndexOf(ControlID) > 0 )
{
ret =aControls;
break;
}
}
}
return ret;
}

You can use it by writing
var txtCity = findControl("City");
 

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
474,265
Messages
2,571,069
Members
48,771
Latest member
ElysaD

Latest Threads

Top