0

I have a call in my codebehind that looks like:

bool isQtyValid = true;
bool disabled = false;
foreach(parameters)
{
    //setting isQtyValid
    if (!isQtyValid)
        disabled = true;
    Page.ClientScript.RegisterStartupScript(this.GetType(), "function", string.Format("SetButtonStatus('{0}')", disabled.ToString().ToLower()), true);
}

then my javascript looks like:

function SetButtonStatus(disabled) {
    var bb = document.getElementsByClassName('ButtonSubmit');
    for (var i = 0; i < bb.length; i++) {
            bb[i].disabled = disabled;
    }
}

For some reason this will only work one way. If it gets set to disabled = true, it won't go back to being enabled, and if it gets set to disabled = false, it won't go back to being disabled. I have looked at the variable to see if it is being set right, and it does change from true to false and false to true when it should, but it doesn't change the disabled state of the button.

Am I missing any little things that you can see?

Thank you for any help you can give me.

4
  • is ! a typo not present on your real code?function SetButtonStatus(!isQtyValid) Commented Jun 20, 2013 at 17:04
  • yes it was, i have updated with a more descriptive view of my code Commented Jun 20, 2013 at 17:06
  • Under what conditions can the state be changed? If the state is changed in an async fashion (i.e. ajax), then the subsequent calls to register your start-up scripts will never be run because that happens only on page load, not on partial postbacks. Commented Jun 20, 2013 at 17:12
  • Is this a webforms app? Is the check for isQtyValid on an event like the form submit click? Commented Jun 20, 2013 at 17:15

1 Answer 1

1

The first thing I detect is that you're passing a boolean as string

Try replacing

"SetButtonStatus('{0}')" // <-- SetButtonStatus('true')

by

"SetButtonStatus({0})" // <-- SetButtonStatus(true)
Sign up to request clarification or add additional context in comments.

1 Comment

ding ding ding, this solves the problem, thank you for pointing it out!

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.