Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How does javascript identify controls on asp.net pages?

0 views
Skip to first unread message

COHEN...@lycos.com

unread,
May 6, 2008, 3:51:49 PM5/6/08
to
I'd like a javascript routine to calculate totals of some column in a
table. But the table is in a asp.net page, and when I do a 'view
source', I see that the names of the controls are not what I expect.
Where can I get info on using javascript with asp.net textbox fields?
Thanks,
Marv

Mark Rae [MVP]

unread,
May 6, 2008, 4:01:50 PM5/6/08
to
<COHEN...@lycos.com> wrote in message
news:69ca671c-553b-4ae7...@x41g2000hsb.googlegroups.com...

If you have an <asp:TextBox /> control called MyTextBox, you refer to it
server-side just with its name e.g.

MyTextBox.Text = "Hello";

However, as you've discovered, this almost certainly isn't what ASP.NET
calls the control by the time it's rendered to the client browser.

Thankfully, this is easily solved. Simply "inject" the name of the control
into your JavaScript dynamically by means of its ClientID property, e.g.

<script type="text/javascript">
var myTextBox = document.getElementById('<%=MyTextBox.ClientID%>');
</script>


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

darrel

unread,
May 6, 2008, 4:06:59 PM5/6/08
to
> I'd like a javascript routine to calculate totals of some column in a
> table. But the table is in a asp.net page, and when I do a 'view
> source', I see that the names of the controls are not what I expect.

Javascript has no concept of ASP.net controls as they are server-side
concepts.

Javascript, being client side, simply sees the HTML that is produced by the
server and delivered to the web browser.

> Where can I get info on using javascript with asp.net textbox fields?

Javascript just see the INPUT field. You'd get the info from the INPUT field
via javascript and the DOM.

Typically you'd use something like getElementByID('IDofYourInputField).value

-Darrel


Mark Rae [MVP]

unread,
May 6, 2008, 4:19:05 PM5/6/08
to
"darrel" <not...@nowhere.com> wrote in message
news:uzG$%23S7rI...@TK2MSFTNGP02.phx.gbl...

> Typically you'd use something like
> getElementByID('IDofYourInputField).value

Typically, this won't work because chances are you'll have no way of knowing
what the name of the DOM element is by the time it's been rendered to the
client browser - see my earlier reply...

darrel

unread,
May 6, 2008, 5:04:24 PM5/6/08
to
> Typically, this won't work because chances are you'll have no way of
> knowing what the name of the DOM element is by the time it's been rendered
> to the client browser - see my earlier reply...

I've found that it's usually the ID of the usercontrol/webcontrol

However, to be accurate, you use the asp.net clientID propert to write the
value into the javascript:

document.getElementByID('<%=yourInputField.ClientID%>').value

(which, I just now see, you already mentioned in your other reply. ;o)

-Darrel


0 new messages