0

I am facing a strange programming problem.

I've a page for user registration. I am using JavaScript code to disable text boxes when user selects no from option.

But after the selecting Option 'No' from drop down list, page is not getting submitted and not redirecting to next page.

And surprisingly, if I select every 'Yes' & fill text box, then it submits smoothly and redirect to next page.

I suspect this is JavaScript problem. Need help please !


Here is my javasctipt code sample

function DisableEnable2() {

        var ddlCnovict = document.getElementById("<%= ddlConviction.ClientID %>")
        var txtConDate = document.getElementById("<%= txtConvictDate.ClientID %>")
        var txtConDetail = document.getElementById("<%= txtConvictionDetails.ClientID %>")


        if (ddlCnovict.options[ddlCnovict.selectedIndex].text == "No") {

            txtConDate.disabled = true;
            txtConDetail.disabled = true;
        }

        else {
            txtConDate.disabled = false;
            txtConDetail.disabled = false;
        }


    }

Asp code:

Yes No

6
  • Please show your code. Commented Mar 27, 2014 at 8:10
  • do you have asp validators on your page? Commented Mar 27, 2014 at 8:50
  • I suspect that the model must require that the textbox values be present before declaring the form as valid i.e. the fields are marked as required.Check any model validations. Commented Mar 27, 2014 at 8:51
  • @kobe, yes. I've validators. Commented Mar 27, 2014 at 8:53
  • i guess you have required field validator on the textboxes, they prevent the sumbit behavior when the textboxes is empty Commented Mar 27, 2014 at 8:54

3 Answers 3

2

disable your validators when you don't need textboxed to be required:

function DisableEnable2() {

    var ddlCnovict = document.getElementById("<%= ddlConviction.ClientID %>")
    var txtConDate = document.getElementById("<%= txtConvictDate.ClientID %>")
    var txtConDetail = document.getElementById("<%= txtConvictionDetails.ClientID %>")

    var txtConDateValidatior = document.getElementById("<%= txtConvictDateValidatior.ClientID %>")
    var txtConDetailValidatior = document.getElementById("<%= txtConvictionDetailsValidatior.ClientID %>")

    if (ddlCnovict.options[ddlCnovict.selectedIndex].text == "No") {
        ValidatorEnable(txtConDateValidatior, false);
        ValidatorEnable(txtConDetailValidatior, false);
        txtConDate.disabled = true;
        txtConDetail.disabled = true;
    }

    else {
        ValidatorEnable(txtConDateValidatior, true);
        ValidatorEnable(txtConDetailValidatior, true);
        txtConDate.disabled = false;
        txtConDetail.disabled = false;
    }


}

I know there are validations by the author's comment response:

@kobe, yes. I've validators. – user2285026 10 mins ago

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks kobe !! You made my day :) I never expected that asp validators mess with submit without any warning/error. It's nice solution..
remember the logic: the validatiors also client side and there is no point to go to the server if the condition is not met
0

So, I have never seen such constructions in HTML

<%= ddlConviction.ClientID %>

It's looks like scriplets on server side and on the client it should be transformed into something different.

I think you have a JS error on the page, because JS can't find

<%= ddlConviction.ClientID %>

and your

txtConDetail 

for example is NULL, so the construction

txtConDetail.disabled

will cause the error.

Try to use DevTools in Chrome or FireBug in FireFox, also see the page source in browser.

1 Comment

Thanks for reply. my script works very fine. problem is with asp validators. @kobe is right.
0

Instead of disabling the control use the attribute function to add the disable attribute:

txtConDate.Attributes.Add("disabled","true");

and on selecting "YES" do this,

txtConDate.Attributes.Clear("disabled");

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.