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

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

Suggested Solutions

Title # Comments Views Activity
I am having a  Git   issue 6 42
Html fieldset fix its height and width 4 33
on window close, not beforeunload 6 18
IDE for Python 5 25
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The viewer will learn how to count occurrences of each item in an array.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

830 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