How to reference a server control from client side Javascript - More Wally - Wallace B. McClure
in

MoreWally.com

Giving people what they want, More Wally. This is the technical and personal blog site of
Wallace B. (Wally) McClure.

This Blog

Syndication

News

Please goy buy 3-4 copies of my book on MonoTouch titled "Professional Android Programming with Mono for Android for .NET/C# Developers." They make great gifts all year round. Plus, I get about $.25 when you buy a copy.

Technical Sites

More Wally - Wallace B. McClure

This blog will have all kinds of posts about Wally McClure. In it, there will be tons of .NET and computer programming posts as well as Wally's views on life in general. As you might guess, this site and blog help you get More Wally in your life. What more could anyone want? iPhone, Android, MonoTouch, MonoDroid, Mobile, HTML5, .NET, ADO.NET, ASP.NET, AJAX, jQuery, jQuery Mobile, ASP.NET AJAX, and Windows Azure............follow me on twitter at Wally

How to reference a server control from client side Javascript

Imagine that you have a text control like this:

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

I see all kinds of examples that get the control valuelike this:

document.forms[0].txtName
---or---
document.getElementByID("txtName")

While it might work in simple situations, it won't work all of the time.  How about this:

document.forms[0].<%=txtName.ClientID%>
---or---
document.getElementByID("<%=txtName.ClientID%>")

by referencing the txtName.ClientID property, you get the client id of the control.  The reason this is important is that if the control is contained within another container, the name is mangled to generate a unique value, which is more than the simple name of the control.

 

Published Sep 22 2007, 09:49 AM by wallym
Filed under: ,

Comments

No Comments
2006 - Wallace B. McClure
Powered by Community Server (Non-Commercial Edition), by Telligent Systems