0

enable and disable asp validator using embedded code in asp tag doesn't work, The value is written but control still is enabled. Please check the occurrences of

Enabled="<%# Convert.ToBoolean(txtText.Enabled) ? false : true%>" 

in this form:

<form id="frmValidator" runat="server">
    <div>
        <asp:ValidationSummary
            ID="Summary"
            runat="server"
            HeaderText="Error(s):"
            CssClass="msg-error" />
        <asp:TextBox ID="txtText" runat="server" MaxLength="15" Enabled="false" />
        <asp:RequiredFieldValidator
            ID="rfvtxtText"
            runat="server"
            ControlToValidate="txtText"
            ErrorMessage="Requiered."
            Display="None"
            ClientValidationFunction=""
            SetFocusOnError="true"
            Enabled="<%# Convert.ToBoolean(txtText.Enabled) ? false : true%>" />

        <asp:RegularExpressionValidator
            ID="revtxtText"
            runat="server"
            ControlToValidate="txtText"
            Display="None"
            ErrorMessage="Invalid."
            ValidationExpression="[a-zA-ZñÑáéíóúÁÉÍÓÚ ,.*]{3,50}"
            SetFocusOnError="true"
            Enabled="<%# Convert.ToBoolean(txtText.Enabled) ? false : true%>" />

        <asp:Button
            ID="btnSave"
            runat="server"
            Text="Save" />
    </div>
<form>
2
  • 1
    txtText.Enabled does not need converting to boolean, doesn't it? Commented Apr 29, 2015 at 16:05
  • Yes, but still is not working Enabled="<%# txtText.Enabled ? false : true%>" the validator continue with a true value Commented Apr 29, 2015 at 16:13

2 Answers 2

1

The answer is you cannot.

<%# %> is Data Binding Expression Syntax. You cannot use it without ServerControl such as GridView, ListView.

Normally, we disable/enable control from code behind.

Javascript Method

Another approach is to disable validation using Javascript. However, you need to redirect to different page or do something after a button click. Otherwise, validation message will be displayed back to user after post back.

<script type="text/javascript">
    if (document.getElementById('<%= txtText.ClientID %>').getAttribute('disabled') === 'disabled') {
        alert('disabled');
        ValidatorEnable(document.getElementById('<%= rfvtxtText.ClientID %>'), false);
        ValidatorEnable(document.getElementById('<%= revtxtText.ClientID %>'), false);
    }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

disable validation using javascript change enebled value to false but still validator continue firing massage, i alert the validator and it is false
0

In my opinion, it should be possible. Though I'm still a rookie myself, I always forget which code-nugget does what. I'd try <%: --expression-- %>.
The second answer to this question does a pretty good summary.

1 Comment

Try with just <% --code-- %>. You should be able to use almost anything anywhere. Just remember to use a semicolon ( ; ) after the code in that nugget, just like in code-behind.

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.