Click here to Skip to main content
11,705,212 members (51,556 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET

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
Edited 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 at 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 at 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 at 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 at 2-Nov-11 1:47am
I already use ClientID,
please show it again i updated the question
AspDotNetDev at 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 at 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 at 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 at 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."
MrProgrammer_78 at 2-Nov-11 17:12pm
If you read my question good you will see the error:
document.getElementById(...) is null or not an object.

I Google "disabled" property man and i used it many times before.

try to tell me the code to solve my problem instead of all that words
my question is clear, give me solution if you know it and i will be happy
thanks for your time

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 525
1 OriginalGriff 244
2 ppolymorphe 126
3 F-ES Sitecore 110
4 Andy Lanng 85
0 OriginalGriff 8,988
1 Sergey Alexandrovich Kryukov 8,272
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 2 Nov 2011
Copyright © CodeProject, 1999-2015
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