0
<script type="text/javascript">
function ValidateProductID(sender, args)
{
    var productID = document.getElementById('<%=txtProductID.ClientID%>').value;
    var productType = document.getElementById('<%=rcbProduct.ClientID%>').value;
    if (productID != "" && productType == "") {
        args.IsValid = false;
    }
    else
        args.IsValid = true;
}

this script for the custom validator is throwing exception .

An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

can someone tell me how to fix it.

grace
  • 253
  • 1
  • 5
  • 17
  • it seems getElementById('<%=txtProductID.ClientID%>') is throwing the exception. is there any other way in javascript to obtain control value?? – grace Mar 24 '15 at 16:10
  • document.getElementById('txtProductID').value is not working either – grace Mar 24 '15 at 16:37

2 Answers2

2

Add ClientIDMode="Static" to those textboxes (or whatever they are) and just use document.getElementById('txtProductID').value;

Per the docs, setting the ClientIDMode to Static means:

The ClientID value is set to the value of the ID property.

That means the ID won't be changed by some algorithm and will be txtProductID instead of something that you have to access with txtProductID.ClientID.

Usually the error you are getting is because you put that script inside of some other control with runat="server" on it. The solution above is likely your easiest solution, though without more of your code it is hard to say.

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
  • This won't work on older versions of the .Net framework. – RandomWebGuy Mar 24 '15 at 16:55
  • Possibly, but still worth noting that ClientIDMode isn't always available. +1 for providing multiple solutions. – RandomWebGuy Mar 24 '15 at 17:43
  • @RandomWebGuy True, but usually if the OP is using an old version of ASP.NET (< 4.0) they will mention that. My answer provides another solution as well (moving the script), though I'm comfortable with solutions that work with the related technology going back 5 years (ASP.NET 4.0 release :). Though yes, your comment is worth noting, as you say. – MikeSmithDev Mar 24 '15 at 17:43
0

Apparently, I had my script out of the Content Template, moved it in with the existing code and it works !

grace
  • 253
  • 1
  • 5
  • 17