Solved

How do I reference an asp.net label id in a javascript function call from asp.net without knowing the generated id for that label

Posted on 2013-01-07
4
593 Views
Last Modified: 2013-11-19
A have a textarea (txtSMSMelding), a JavaScript function (taCount) counting the number of characters in the textarea and a label (lblAntTegn) containing the counter.

The counting is done as I am typing in the characters.
The problem is that I have to know the exact ID of the the counter generated for the browser.

In the code example below I am transfering the label with the ID "lblAntTegn" to the JavaScript function "taCount". But because the label is inside a usercontrol named ucSMS, witch in turn is inside another usercontrol  named ucMerInfoForetak, I have to type in the object name 'ucMerInfoForetak_ucSMS_lblAntTegn'.

This means that if I am moving this code somewhere I have to change the object name passed to the JavaScript function.
How can I specify this object name more generally in the code below?

                          <textarea id="txtSMSMelding" runat="server" onkeypress="return taLimit(this);" onkeyup="return taCount(this,'ucMerInfoForetak_ucSMS_lblAntTegn');"
                                        name="Description" rows="10" cols="50">

                                    </textarea>
0
Comment
Question by:Joar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 38750177
add the following JS snippet:

<script type="text/javascript">

  var lblAntTegnID = '<%= lblAntTegn.ClientID %>';

</script>

Open in new window


now u can reference your label via lblAntTegnID variable.
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 38750195
<textarea id="txtSMSMelding" runat="server" onkeypress="return taLimit(this);" onkeyup="return taCount(this,'<%= lblAntTegn.ClientID %>');"
                                        name="Description" rows="10" cols="50">

</textarea>
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 38750200
ultimately, you'll have something like:

<textarea id="txtSMSMelding" runat="server" onkeypress="return taLimit(this);" onkeyup="return taCount(this, '<%= lblAntTegn.ClientID %>');"
                                        name="Description" rows="10" cols="50">

                                    </textarea>

Open in new window

0
 

Author Closing Comment

by:Joar
ID: 38750209
That simple!
I feel like a fool when moving into the JavaScript world...
Did function!
Thanks!!
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question