QUERY: JavaScript vs. ASP.NET Web/Server Controls

Joined
Mar 28, 2006
Messages
15
Programming Experience
Beginner
Is it possible for JavaScript to access the DOM of ASP.NET Web/Server
Controls?

For example, I have a legacy HTML forms, controls, etc. If I convert all
my HTML into Web Controls (by adding runat="server & id="") then will
this affect my JavaScript code that accesses/modifies the DOM of the page
(DOM tables, childs, elements, traversing, etc.) If so, what is the best
way to make them compatible again?
 
The problem you will encounter is that the ID's will change based on the page hierarchy. One technique to keep your javascript working is to not put any names in your javascript. Use a variable name in you JS and then when ASP.NET writes controls that you want to access w/JS, register a script that writes the ID of the control mapped to the variable. For example:

Imagine we want to alert the user on page_unload if a control has an empty value... the JS could look like this:
VB.NET:
<body onunload="javascript: if(ctlToCheck.value==''){alert('Value is blank');};">

Note that the ctlToCheck variable should not be set in the page or JS file... you will auto-generate it by ASP.NET via RegisterClientScriptBlock... i.e.
VB.NET:
'Remember this is in the VB.NET code-behind
Page.RegisterClientScriptBlock("jsForCtlToCheck", "<script language=""javascript"">var ctlToCheck = document.getElementById('" & Me.CtlToCheck.ClientID & "');</script>")

or something like that...
 
Back
Top