1

having a bit of an odd problem, the situation is a bit unique in that I want to show/hide a div in instances both server side and client side, as such I cant change it to a panel.

The current code I have to change its visiblity client side, which works, is:

$('#<%= txtSurname.ClientID%>').on('input', function hideControl() {

        var current = $('#<%= txtSurname.ClientID%>').val();
        var surname = $('#<%= hdnSurname.ClientID%>').val();

        if (current == surname) {
            $('#pnlReason').hide()
            console.log('hiding');
        } else {
            $('#pnlReason').show()
            console.log('showing');
        }

    });

When a user clicks a button, the page validates, and refreshes, and the panel is rendered invisible again. As such I want to run this code again on the page load so that if the two variables are still different when the page validation is run, the panel is still visible. This is what im using to call it serverside:

 Page.ClientScript.RegisterStartupScript(Page.GetType(), "ShowHide", 
 "$(document.ready(hideControl()));", True)

When I try running it however, it says that hideControl is undefined, any ideas whats going wrong?

1
  • $('#<%= txtSurname.ClientID%>').on('input', function hideControl() { is wrong.. If you have this code in js file and if the same has been included in the page then no need to call it through Page.ClientScript and the above line should have been with anonymous function like this one $('#<%= txtSurname.ClientID%>').on('input', function() { Commented Oct 22, 2015 at 9:47

2 Answers 2

1

you could reorganise your jquery like this and once the page is refreshed, you can execute your function at the end of the document ready without the need for RegisterStartupScript():

//shorthand for document.ready
$(function () {
    var hideControl = function() {
           var current = $('#<%= txtSurname.ClientID%>').val();
           var surname = $('#<%= hdnSurname.ClientID%>').val();

           if (current == surname) {
               $('#pnlReason').hide()
               console.log('hiding');
           } else {
               $('#pnlReason').show()
               console.log('showing');
            }
       }

    $('#<%= txtSurname.ClientID%>').on('input', function () {
        hideControl();                    
    });

    //call it at end of ready function:
    hideControl();
};
Sign up to request clarification or add additional context in comments.

5 Comments

Looks good, although hideControl is still coming up as undefined?
Are you still using the registerstartup method?
Ah my bad, removed that and tried it entirely from the client side, however when the save button is pressed the function isnt being run?
Assuming you have the doc ready function etc, when the post is complete it should rerun the script again. Also ensure the script is between the <script> tags
Copy and paste the code I provided as is and see what you get
0

Since hideControl is defined as anonynous function you can't access it. You have to define the function hideControl outside of the on event handler. Put the function within the script tags in the page not in a separate file:

<script>
     function hideControl() {
    var current = $('#<%= txtSurname.ClientID%>').val();
    var surname = $('#<%= hdnSurname.ClientID%>').val();

    if (current == surname) {
        $('#pnlReason').hide()
        console.log('hiding');
    } else {
        $('#pnlReason').show()
        console.log('showing');
    }

}
</script>

Then call the function like that at server side without the parenthesis:

Page.ClientScript.RegisterStartupScript(Page.GetType(), "ShowHide", 
"$(document.ready(hideControl));", True)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.