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
591 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

732 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