Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
Hi guys,
 
I am using Javascript in ASP.NET project,
Javascript code is in separate file and in asp.net page i write that:
 
<script type="text/javascript" src="js/myfile_settings.js"></script>
 

<div id="diTextArea">
                    <script type="text/javascript" >
                     initiate(window.document);
                    </script>
</div>
 
ASP.NET page is content page inside master page
 
initiate method in js file is used to inject or render html code within the div control.
 
When i run the project that error appear:
 
getElementById(...) is null or not an object.
 
I make small project and used the js file and the same html code and master page and it run successfully.
but in my big project it is not run successfully.
 
no difference between two project, the same js file and html code but one has many pages and the other is one.
 
js file has this method:
page.getElementById("diTextArea").innerHTML=html_code;
 

 
realy i tired...
Posted 6-Sep-11 21:10pm
Edited 8-Sep-11 5:05am
v7
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

check your control name which you are passing to getElementById. getElementByID except a paramenter   as control name which is case sensitive   so please check that. if your   aspx page is content page of master page then your control id will append the master page id also
so please go to the page and then right click get viewsource of page copy the id of the contol and paste it in java script.
  Permalink  
Comments
MrProgrammer_78 at 7-Sep-11 3:54am
   
Thanks man,yes my aspx page is content page of master page,
and i did what u told me and the same problem still,
that is the id i copy it from view source of page "MainPlaceHolder_OperatorPlaceHolder_diTextArea"
I try every thing but no way
pooja 08 at 7-Sep-11 4:59am
   
instead of page try with document.form.getElementById and 1st keep one alert box before assigning any value to that control check document.form.getElementById (your controlid) &check what you are getting
rajeevtechnical at 7-Sep-11 5:34am
   
document.getElementById("MainPlaceHolder_OperatorPlaceHolder_diTextArea").innerHTML=wysiwyg_code;
This I believe should work.
MrProgrammer_78 at 7-Sep-11 5:58am
   
Not help also
Elina Blank at 8-Sep-11 17:16pm
   
To be sure what the id of the control is, do "View Source" and find that element. Then do "document.getElementById()" with passing exactly the id you saw in the View Source
pooja 08 at 7-Sep-11 6:14am
   
did you placed alert box?
alert(document.getElementById("MainPlaceHolder_OperatorPlaceHolder_diTextArea"));
& check what it is showing?
Is it showing null or object?
if it is showing object then problem is in other place?
reply what you are getting
MrProgrammer_78 at 7-Sep-11 8:01am
   
It give me Null,
i put that in js fie:
//alert("dsfgsdfg"); it is as comment
alert(page.getElementById("diTextArea"));
 
the first line i mark it as comment for put your alert statement,
but when run it still give me the first alert ??????
 
I think that Visual Studio 2005 which i use is itself cause the problem
i think i must upgrade to Visual Studio 2008 or 2010,
is it true ?
Elina Blank at 8-Sep-11 17:17pm
   
This may be a caching issue
MrProgrammer_78 at 8-Sep-11 21:59pm
   
how to prevent this caching issue ???
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
There could be a chance that your control names are being changed by ASP.NET when submitted to the client-side seeing that you are using master and content pages.
 
Change your webconfig to include:
 <system.web>
      <pages clientidmode="Static"></pages>
 </system.web>
 
that way asp.net will not mess with your control ids!
 
Goodluck
  Permalink  
Comments
MrProgrammer_78 at 7-Sep-11 5:58am
   
I include it but it give me error that:
Unrecognized attribute 'clientidmode'
Morgs Morgan at 7-Sep-11 6:01am
   
Sorry about that, it's case sensitive so go like so:
<pages clientIDMode="Static"></pages>
MrProgrammer_78 at 7-Sep-11 6:04am
   
I know it case sentive man, so i try every like:
clientIDMode,ClientIDMode,clientIdMode,clientIdmode,clientIDmode...etc
 
but all not help also.
Morgs Morgan at 7-Sep-11 6:13am
   
What .Net framework are u using? this property is only supported in .Net 4.
if you are using anything less than 4 then try accessing your controls by their class names using jQuery e.g.
 
<div class="div1" önclick="SetInnerHtml();"></div>
<script type="text/javascript" >
function SetInnerHtml()
{
$('.div1').html('wysiwyg_code');
}
</script>
MrProgrammer_78 at 7-Sep-11 6:13am
   
I am using ASP.NET 2.
I think this attribute clientIDMode is used is ASP.NET 4,
is it right?
MrProgrammer_78 at 7-Sep-11 6:17am
   
at the same moment hhhhh,
thank man, i appreciate your help,
but i do not know jquary, can i use class by javascript?
MrProgrammer_78 at 7-Sep-11 6:30am
   
I change my initiate method to return variable wysiwyg_code that represent html code,
This is my try :
 
<div class="div1" ></div>
<script type="text/javascript" >
function SetInnerHtml()
{
$('.div1').html(initiate());
}
</script>
but not worked with me.
I do not want to render the html code within div when click it but when page loaded,
can u adjust my JQuary code please as i do not know JQuary.
thank you
Morgs Morgan at 7-Sep-11 6:33am
   
hi,
send me an e-mail and attach your test.aspx page and your myfile_settings.js file then i can help u maybe.
e-mail: msimwaba@gmail.com

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 598
1 George Jonsson 268
2 OriginalGriff 215
3 CPallini 210
4 PIEBALDconsult 155
0 OriginalGriff 5,865
1 Sergey Alexandrovich Kryukov 5,316
2 CPallini 4,760
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 8 Sep 2011
Copyright © CodeProject, 1999-2014
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