1
System.Web.HttpException: The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

As soon as I remove my Javascript code, it works fine. What is wrong with my script?

<script type="text/javascript">
    function ShowImagePreview(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#<%=ImgPrv.ClientID%>').prop('src', e.target.result)
                        .width(240)
                        .height(150);
            };
            reader.readAsDataURL(input.files[0]);
        }
    }
    function preventBackspace(e) {
        var evt = e || window.event;
        if (evt) {
            var keyCode = evt.charCode || evt.keyCode;
            if (keyCode === 8) {
                if (evt.preventDefault) {
                    evt.preventDefault();
                } else {
                    evt.returnValue = false;
                }
            }
        }
    }
    function SetContextKey() {
        $find('<%=acestate.ClientID%>').set_contextKey($get("<%=txtRef4.ClientID %>").value);
    }
    $(document).ready(function () {
        window.history.forward(1);
    });
</script>

EDIT:

I have googled it and found solution i.e., replace <%= with <%#, it's working but when I replace it then the image preview javascript code is not working. So, please provide me different solution.

  • What is the output of `acestate.ClientID` and `ImgPrv.ClientID`. View the page source and see what it looks like once rendered – Jamie Rees Jul 27 '15 at 08:28
  • I have two web method first for fetching city and second for state for that I have use it. –  Jul 27 '15 at 08:30
  • `System.Web.HttpException: ` - are you sure that's a JAVASCRIPT error - looks more like a C# error to me – Jaromanda X Jul 27 '15 at 08:32
  • 1
    possible duplicate of ["The Controls collection cannot be modified because the control contains code blocks"](http://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-bl) – user1666620 Jul 27 '15 at 08:33

1 Answers1

0

Nothing is wrong in your script. The error is because the control is not yet rendered, while the Javascript code is executed.

Are you using any Ajax control ? If so, then removing it will also remove the error. Well another solution is Just remove the script from head section and put in body part.

Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40