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 8908441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:10:45+00:00 2026-06-15T03:10:45+00:00

I’d like to be able to get a ASP.NET control’s ClientID inside of a

  • 0

I’d like to be able to get a ASP.NET control’s ClientID inside of a JavaScript function. I also would like to be able to pass in a var containing a control name so that different controls can use the same function.

For example, if I wanted to use a control named Button1, this works fine:

function doStuffToButton1Control()
{
    var buttonControl = document.getElementById('<%= Button1.ClientID %>');
    //do stuff with buttonControl
}

But I get an error when I try to make the control name dynamic. I’m trying to string together the ClientID call, like so:

function doStuffToGenericControl(controlName)
{
    var dynamicControl = document.getElementById('<%= ' + controlName + '.ClientID %>');
    //do stuff with dynamicContorl
}

I’ve tried using double quotes or single quotes when creating the string, but the error is always “Too many characters in string literal” or “Too many characters in character literal”. The control exists and the name passes correctly (as tested by an alert(name)).

Is there a better way to go about this, or am I missing something obvious?

Thanks!

Update: This function will be on a master page. Any number of child pages will be calling it. For example, one child page might have an ASP control that calls it like so:

<asp:LinkButton ID="Button2" runat="server" OnClientClick="doStuffToGenericControl('Button2')">...</asp:LinkButton>

But another page might have this ASP control that also calls it:

    <asp:LinkButton ID="Button4" runat="server" OnClientClick="doStuffToGenericControl('Button4')">...</asp:LinkButton>

However, these controls’ IDs often change by ASP.NET on render, so they end up looking, for example, like MainContent_Button2 instead of just Button2. To get around this, I traditionally just used <%= Button1.ClientID %>, but since I want any number of controls to be able to use this one function, I’m having a bit of trouble.

Update 2: I was able to use this along with a getAttribute call to get what I needed. (Obviously this will only work when the control you are referring to is the one you are clicking, but that is what I needed) Like so:

function doStuffToGenericControl(controlName)
{
     var controlID = controlName.getAttribute('id');
     var control = document.getElementById(controlID);
     //do stuff with control
}
  • 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-06-15T03:10:46+00:00Added an answer on June 15, 2026 at 3:10 am

    ‘<%= Button1.ClientID %>' is something that replaced in with control id on asp.net page render,

    but when you pass variable in client side, you should pass id and use it without any % scops

    so try this

    function getGenericControl(control)
    {
        var dynamicControl = control;
        alert(dynamicControl);
        //do stuff with dynamicContorl
    }
    

    UPDATE

    You can use <%= ClientID %> to generate parameters for the method above, so just change OnClientClicks like this and use method above

    <asp:LinkButton ID="Button2" runat="server" OnClientClick="doStuffToGenericControl(this)">...</asp:LinkButton>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace

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.