Here is where the request/response paradigm comes into play. The problem is that once the page is sent back to the client you have no control what the user changes unless you send the page back to the server (autopostback in ASP.net) s.t. you can check for changes. But that's "ugly" and may impact on the user-friendliness of your app since it causes a complete page refresh (your customer wouldn't be happy for sure). Ajax is a must in that case. Google is an expert in that field. Just take a look at some of its services like Gmail, GReader or GDocs, just to mention some of them or the Google Code pages.
But back to the original problem (wherefore I'm writing this post :) ): if you have to show/hide controls on your UI, you usually won't need any Ajax (unless the conditions are complex and are calculated by some business logic on the server-side). The temptation however is big ;) , doing it with an UpdatePanel would be a matter of a minute. Just put the involved controls inside it and add the appropriate event-handlers and server-side code for showing/hiding your fields. And still I strongly discourage this for the above mentioned reasons.
var ddlContract = document.getElementById('<%= ddlContract.ClientID %>');
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)Damn...but actually, the error message is completely justified. On the project I'm currently working, validation controls are assigned dynamically to the UI depending on rules defined in the business logic. That's why that error appears.
After informing myself on the web I came up with basically two workarounds:
- using <%# ... %> instead of <%= ... >
The difference between the two is that <%# ... %> tags are embedded at runtime whereas <%= ... %> is embedded as a part of the generated ASP.net parse tree class. <%#...%> works however only on databound fields.
- Force your scripts to run on a server-control...
...and removing it in this way from the content container where the controls (in my case the validators) are added dynamically at runtime.
var dropDownElement = document.getElementById('<%= ddlContractType.ClientID %>');
//do some actions