Master pages and server control ID - asp.net 2.0

G

Guest

Hi,

I use master pages for ensuring a consistent layout on my app.

I need to add client side functionality to my server controls. I do this in
page_load (server side) like this:
DropDownList1.Attributes.Add("onchange", "Test();");

I add a javascript function ("Test") to the page that changes the disabled
property of several server controls when DropDownList1 has changed. In an
ordinary aspx page (with no master) it is easy to figure out the ID of the
server controls (e.g. "Button1") but when a control is placed in a content
page, the ID becomes something like
"aspnetForm.ctl00_ContentPlaceHolder1_Button1". In what way is the ID
generated? Can I be sure that once the application is deployed, the ID will
be the same?

Thanks, Dorte
 
K

Karl Seguin

Depending on how you place controls, you might be assured of some things,
but it's safer not to assume anything. For example, the ID controls within
user controls can change from page to page. Typically the way I handle this
is to write code as such:

<script language="JavaScript">
var name = document.getElementById("<%=NameTextBox.ClientId%>");
</script>


In case you are wondering, it's because ID's in html must be unique, but in
ASPX, you could have two user controls with a textbox named "name". ASP.Net
solves this problem by appending the hierarchy of controls to the rendered
id.

Karl
 
G

Guest

Thanks for your suggestion, but I'm not quite sure I understand (have tried
it out and can't make it work).
What exactly do 'NameTextBox' and 'ClientId' refer to? Let's say I have a
server control with the ID 'TextBox1', what should I write?

Dorte
 
K

Karl Seguin

var TextBox1 = document.getElementById("<%=TextBox1.ClientId%>");

the TextBox1 variable in javascript will now reference the DOM object
created by the server side object.

Karl
 
G

Guest

I wrote this in a javascript section:
var txtPhone = document.getElementById ("<%=txtPhoneNumber.ClientId%>");
and the compiler says:
System.Web.UI.WebControls.TextBox does not contain a definition for
'ClientId'. Are you sure this applies to ASP.NET 2.0?

Dorte
 

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,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top