views:

27

answers:

1

I've been looking on the Internet for a fairly clear explanation of the different methods of registering javascript in an asp.net application. I think I have a basic understating of the difference between registerStartupScript and registerClientScriptBlock (the main difference being where in the form the script is inserted). I'm not sure I understand what the RegisterClientScriptInclude method does or when it is used. From what I can gather, it is used to register an external .js file. Does this then make any and all javascript functions in that file available to the aspx page it was registered on? For example, if it was registered in the onLoad event of a master page, would all pages using that master page be able to use the javascript functions in the .js file? What problems would arise when trying to use document.getElementById in this case, if any? Also, when it is necessary/advantageous to use multiple .js files and register them separately?

I appreciate any help you can give. If you know of any really good resources I can use to get a thorough understanding of this concept, I'd appreciate it!

A: 

Here's an article on referencing JavaScript from master and content pages:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=273

IrishChieftain
Thanks for the resource. It helps a little. I think I'll have a better understanding if I tell you what I'm trying to do. I currently have a master page. In the preRender event, I'm doing RegisterClientScriptInclude to register my external .js file. On a content page, I'm tyring to use document.getElementById('<%=TextBox1.ClientId%>').value. This is not working. However, when I use getElementById('ctl00_ContentPlaceHolder1_TextBox1'), it does work. Can you explain why? I'm guessing it has to do with when the JS is being inserted on the page.
mpminnich
Take a look at the syntax in step 5 of the linked tutorial - it shows how to use JavaScript referenced from a master page to find a control in a content page :-)
IrishChieftain
Ok. I can get this to work using the <%=%> server control clientid method as long as the script is on the content page. When I refer to the .js file on the master page, I get a null reference exception. I'm not sure why (because I don't know exactly what is going on behind the scenes and when). Also, what would I need to do to get it to work by registering the .js file via Page.ClientScript.RegisterClientScriptInclude method? I appreciate your help (and your patience). Again, any other tutorials/resources that you could recommend would be great.
mpminnich
I would try debugging it to find out the order in which things are happening: http://weblogs.asp.net/scottgu/archive/2007/07/19/vs-2008-javascript-debugging.aspx
IrishChieftain