Click here to Skip to main content
13,093,097 members (63,773 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I want to use javascript with dynamic controls,
but the problem javascript method cannot see the dynamic control in the page, it give me error that:

document.getElementById(id) is null or nit an object

i pass the id of control to javascript method correctly, as i use alert to show the id and it right.
and all code in javascript method work right except this line

this is my method:

function SpecificMethod(id)
   // some code here work fine
  alert(id);  // give me right id 
  document.getElementById(id).disabled =true;  // give error

and this is the code which create the control dynamically:

protected void Page_Load(object sender, EventArgs e)
        LinkButton lbtnMyLink = new LinkButton();
        lbtnMyLink.Text = "Hello world";
        lbtnMyLink.ID = "lbtnMyLink_1";
        lbtnMyLink.OnClientClick = "SpecificMethod(" + lbtnMyLink.ClientID + ");return false";
Posted 1-Nov-11 8:29am
Updated 1-Nov-11 19:45pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Just because you get an id doesn't mean it is correct. Debug you script, look at the source to verify the actual id for the DOM element you are trying to use.

Since you have given us no clue as to what this control is or how it is used, remember ASP.NET will mangle the name. The id you assign in the markup may not be the same as what is rendered in the browser.
MrProgrammer_78 1-Nov-11 19:50pm
Thanks man, but i tried one to pass (this) object to the method and it give the id as it rendered in the browser, and the problem still.
and i tried another one to pass id as it in server code and the same problem also.
Mark Nischalke 1-Nov-11 20:13pm
It may be helpful if you edit your question and post the markup you are using and show how you are calling the function
MrProgrammer_78 2-Nov-11 1:46am
I update my question,
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

The server-side ID and the client-side ID are not the same. Try something like this:
document.getElementById("<%= serverSideControl.ClientID %>").disabled = true;

If you want to see an example client-side ID, just run your web application and right-click in IE and select "view source". That will show you the HTML that the ASP.Net renders to.

Also, do NOT hardcode the client-side ID you see... the client-side ID can change depending on a number of factors. Just use the ClientID property to get the client-side ID dynamically.
MrProgrammer_78 2-Nov-11 1:47am
I already use ClientID,
please show it again i updated the question
AspDotNetDev 2-Nov-11 2:00am
A LinkButton will render in HTML as an anchor tag (an "A" tag). Anchor tags don't have a disabled property. Depending on what you are trying to acheive, you can add a click handler to the anchor tag that just returns false. That will prevent the click from happening. You may also want to add a class or some style that makes the anchor look disabled (e.g., gray rather than blue). You could also set the style to "display: none;" and that would completely hide the anchor tag... you could then show some text in its place (and maybe style it too look like an anchor tag that is disabled).
MrProgrammer_78 2-Nov-11 3:45am
Thanks man, but which LinkButton render as anchor, are you mean dynamic LinkButton or LinkButton as general,
I can disable LinkButton easy but in this case where i use dynamic control it has problem.
and i tried to use "display: none" and also give me the same error.
AspDotNetDev 2-Nov-11 11:13am
Did you Google how to set the display property to "none" before you tried to do it? It's a CSS property, so it's different than how you would set, for example, the "disabled" property.
AspDotNetDev 2-Nov-11 11:13am
Also, make note of the exact error you are being given, rather than just saying "give me the same error."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 2 Nov 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100