Click here to Skip to main content
14,634,269 members
Articles » Web Development » ASP.NET Controls » General

Tips About Using JavaScript in ASP.NET

Rate this:
4.87 (13 votes)
Please Sign up or sign in to vote.
4.87 (13 votes)
9 Sep 2010CPOL
Here are some tips about using JavaScript in ASP.NET

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 its 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 do 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 placeholder, 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 its source HTML using Internet Explorer, 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:

document.getElementById('id');

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

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.

  • 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:
  • 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 provides 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:

You want to debug your JavaScript code and don’t know how?!! Here you have the solution: All you have to do is place all your JavaScript code in a separate .js file, so you’ll be able to insert breakpoints inside the JavaScript code, and then enable JavaScript debugging from your Internet Explorer. Here 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.

Image 1

License

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

Share

About the Author

Ahmed_Said
Software Developer Asset Technology Group
Egypt Egypt
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

Comments and Discussions

 
Questionbest way of learnign java script Pin
$r!dh@r15-Aug-13 9:14
Member$r!dh@r15-Aug-13 9:14 
GeneralMy vote of 5 Pin
Keeame10-Jul-13 18:19
MemberKeeame10-Jul-13 18:19 
QuestionNice Explanation ThanQ Pin
sathishvenkat20-May-13 0:00
Membersathishvenkat20-May-13 0:00 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey29-Mar-12 19:13
professionalManoj Kumar Choubey29-Mar-12 19:13 
GeneralMy vote of 5 Pin
CiKamaNdeela17-Oct-11 23:41
MemberCiKamaNdeela17-Oct-11 23:41 
GeneralRe: My vote of 5 Pin
Ahmed_Said5-Jan-12 17:16
MemberAhmed_Said5-Jan-12 17:16 
Generalhmm. Pin
MikkelAndersen2-Mar-11 4:36
MemberMikkelAndersen2-Mar-11 4:36 
GeneralRe: hmm. Pin
Pavan Navule7-Oct-12 22:11
MemberPavan Navule7-Oct-12 22:11 
GeneralMy vote of 5 Pin
k.anantharengan24-Sep-10 18:36
Memberk.anantharengan24-Sep-10 18:36 
GeneralRe: My vote of 5 Pin
Ahmed_Said24-Sep-10 23:26
MemberAhmed_Said24-Sep-10 23:26 
GeneralMy vote of 5 Pin
geethamw0313-Sep-10 18:43
Membergeethamw0313-Sep-10 18:43 
GeneralRe: My vote of 5 Pin
Ahmed_Said13-Sep-10 22:42
MemberAhmed_Said13-Sep-10 22:42 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Technical Blog
Posted 9 Sep 2010

Tagged as

Stats

28.7K views
14 bookmarked