Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1042271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:28:55+00:00 2026-05-16T15:28:55+00:00

Good day. I am trying to fetch an asp.net label control’s text value using

  • 0

Good day. I am trying to fetch an asp.net label control’s text value using javascript. However the control id is returned as null when I use

var a = document.getElementById('<%= lblTreatyNo.ClientID %>').innerText;

However it does work when I use the full name from source like

var a = document.getElementById('ctl00_ContentPlaceHolder1_lblTreatyNo').innerText;

I am using master pages in my site design. I want to know the correct way of fetching this. Isn’t there a way not to use the full name from the source file?

Update here is my code sample

<asp:Label ID="lblTreatyNo" runat="server"></asp:Label>    
<asp:Button ID="btnNewSec" runat="server" OnClientClick="javascript:void(window.open ('ft_newsec.aspx?ftid='+document.getElementById('ctl00_ContentPlaceHolder1_lblTreatyNo').innerHTML+'',null,'height=220,width=300,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes')); return false;" Text="Add New Company" CausesValidation="False" Width="155px" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:28:56+00:00Added an answer on May 16, 2026 at 3:28 pm

    The only problem that I can see is that you’re trying to get the element before the DOM is loaded. An actual scenario would be if the asp:Label is defined after the JavaScript code; by the time the JavaScript code is executed, the element you’re trying to get is not known, and document.getElementById will return null. But then again, the second approach shouldn’t work neither in this case. We’d need to know more about the context to help you, i.e. the code where both the asp:Label and the JavaScript code are defined; it looks like something else might be wrong.

    Regarding your question at the end, using the ClientID property is the right way to do it, since ClientID will be the value of the element’s id attribute (HTML).

    Update, in regard to Popo’s newly posted code

    lblTreatyNo.ClientID in the OnClientClick property (in the ASPX source) isn’t rendered; if you have a look at the source code of the rendered page, you’ll see something like the following:

    onclick="&lt;%= lblTreatyNo.ClientID %>
    

    No server-side code should be put in the OnClientClick when defined in an ASPX page, since the property’s value won’t be rendered. You can imagine that OnClientClick will be directly replaced by the JavaScript event handler onclick once the page is rendered, without rendering OnClientClick‘s value.

    To circumvent this problem, you either can set the OnClientClick property in the code-behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        btnNewSec.OnClientClick="javascript:void(window.open ('ft_newsec.aspx?ftid='+document.getElementById('"+lblTreatyNo.ClientID+"').innerHTML+'',null,'height=220,width=300,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes')); 
    
        return false;
    }
    

    Or, if you want to do it on the client-side, you could do the following with jQuery:

    $(function() {
        $("#<%= btnNewSec.ClientID %>").click(function(e) {
            window.open('ft_newsec.aspx?ftid=' + document.getElementById('<%= lblTreatyNo.ClientID %>').innerHTML + '', null, 'height=220,width=300,resizable=yes,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes');
            e.preventDefault();
        });
    });
    

    Once the DOM is loaded, the above defined jQuery code will attach your JavaScript code to the button’s click event; once the user hits the button, your JavaScript code will be executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day all, I am trying to deploy my ASP.NET MVC3 app to my
Good day all; I am trying to manually (not using sublayers) draw images within
Good day, I am having an issue trying to get the Text on a
Good day, I've just started teaching myself VB.net. I'm trying to create a simple
Good day guys, I'm trying to submit a form with a file using jQuery
Good day. I'm trying to make an online quiz application using php. The idea
Good day, I have a question here. I am trying to fetch image from
Good day, I'm trying some examples to classify text from the NLTK cookbook and
Good Day All we are trying to do is inside a trigger make sure
Good day - several days have passed while i was trying to transport my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.