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
583 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
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ā€¦
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now