Click here to Skip to main content
Click here to Skip to main content

Tips about using JavaScript in Asp.Net

By , 9 Sep 2010
 

One of the most annoying issues you may face while using JavaScript in an Asp.Net application is referencing a server control in a page that inherits from a Master page. As an example, say we have an asp textbox server control and we want to get it’s text, if you’ve tried to do this using the document.getElementById(‘controlId’) function, you’d get the error message "object expected" or "object is null;" so why we get those errors while we are sure that the control’s ID and the JavaScript syntax are correct?! Well, the server controls’ IDs are changed in runtime, and they are concatenated with a prefix which is their content place holder, which in our case it would be the content of the master page and you can be sure from this if you run any page and view it’s source HTML using IE, try to search for any server control’s ID, you will find that it’s completely different from the one in the aspx file, so you have two choices in order to reference these controls correctly:

  • First: you can view the HTML source code of each page and find the new generated IDs for your controls and place them in the JavaScript function:
  • document.getElementById('id');

    But this way is very poor and not flexible at all.

  • The Second one: is using the encoding techniques to get the new generated ID programmatically; any asp server control contains a property called ClientID which provide us with the new generated ID for the control at run time, but we want to get this ID in the JavaScript code; so all we’ve to do is doing that:
  • var txtBox = document.getElementById('<%=txtName.ClientID%>'); The symbols: <%=%>means that you can write any C# code inside them, so this way is very easy and flexible for anybody.

You want to debug your JavaScript code and don’t know how?!! Here you are the solution: All you have to do is placing all your JavaScript code in a separate .js file, so you’ll be able to insert breakpoints inside the JavaScript code, and then enabling JavaScript debugging from your Internet Explorer; here you are the steps:

  1. From Tools menu choose Internet Options, then Advanced Tab.
  2. Then under Browsing uncheck the disable script debugging (Internet Explorer) & (Other).
  3. That’s all, insert your breakpoints into your JavaScript file, press F5 to Debug and enjoy.

License

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

About the Author

Ahmed_Said
Software Developer Asset Technology Group
Egypt Egypt
Member
I'm a professional components designer, web developer, UX engineer and 3d designer as well, I'm 4 years experienced .net software engineer and 7 years experienced 3d designer using 3D Max. I'm very interested in RIA technologies, prototyping and UX engineering.
 
Ahmed Said
Senior .Net Software Engineer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5 Pinmembermanoj kumar choubey29 Mar '12 - 19:13 
Question.NET Barcode for ASP.NET Pinmembersteve7g12 Jan '12 - 16:58 
GeneralMy vote of 5 Pinmemberjonanirina17 Oct '11 - 23:41 
Generalhmm. PinmemberMikkelAndersen2 Mar '11 - 4:36 
GeneralMy vote of 5 Pinmemberk.anantharengan24 Sep '10 - 18:36 
GeneralMy vote of 5 Pinmembergeethamw0313 Sep '10 - 18:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130513.1 | Last Updated 9 Sep 2010
Article Copyright 2010 by Ahmed_Said
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid