Search code examples
javascriptasp.netparametersinline-code

Why is my javascript parameter giving me a "It may be inaccessible due to a protection level." error?


My code

<script type="text/javascript" language="javascript">
        function jsFullPath(relPath) {
            var hidefield = document.getElementById('HiddenField1');
            hidefield.value = relPath;
            var fullPathStr = '<%= fullPath(hidefield.value) %>';
        }
</script>

Public Function fullpath(ByVal relPath As String) As String

    Dim fullPathStr As String = Server.MapPath(relPath)
    Return fullPathStr
End Function

Everytime I compile I get that error. I don't understand why. It should work.


Solution

  • It looks as if you are trying to pass a JavaScript variable to your VB method. This unfortunately will not work, as the page has already been parsed and output to the browser (already left the server) by the time your JavaScript is being executed.

    One option would be to retrieve this value through the use of ajax.

    If you already have the value of the hidden field, can you do something like this?

    <script type="text/javascript" language="javascript">
            function jsFullPath() {
                return '<%= Server.MapPath('HiddenField1.Value') %>';
            }
    </script>