Enable/Disable Required field validator from cs page?

I have two TextBox and two Buttons in my page.

One is hidden and the other one is displayed.

When I click the Button1, it will save data of the two TextBox and will validate each TextBox by the RequiredFieldValidator.

Then when I click Button2, it will just hide itself (Button2) and will show the hidden TextBox.

Both TextBox has RequiredFieldValidator validating against Button1‘s Event click.

Now my issue is when I simply enter text to the 1st TextBox and click save, the button click is validating the required field for hidden field. I just want to validate the 2 textbox when it is showing.

How can I avoid this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Well you can simple use the Enabled="false" property of RequiredFieldValidator.

Your markup would look something like this based on your Question.

<asp:TextBox runat="server" ID="tb1"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfv1" ControlToValidate="tb1" ErrorMessage="*" ValidationGroup="gvSave">
</asp:RequiredFieldValidator>

<asp:TextBox runat="server" ID="tb2" Visible="false"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ID="rfv2" ControlToValidate="tb2" ErrorMessage="*" Enabled="false" ValidationGroup="gvSave">
</asp:RequiredFieldValidator>

<asp:Button runat="server" ID="btn1" Text="Save" onclick="btn1_Click" ValidationGroup="gvSave"/>
<asp:Button runat="server" ID="btn2" Text="Show" onclick="btn2_Click" />

And your codebehind like this:

protected void btn2_Click(object sender, EventArgs e)
{
    tb2.Visible = true;
    rfv2.Enabled = true; // Enables the second requiredfieldvalidator
}

protected void btn1_Click(object sender, EventArgs e)
{
  // your Saving code here
}

Method 2

use the ValidationGroup="group" property to button and assign validation group to text on which you want to validate.
Hope it will help

Method 3

You can specify CausesValidation="false" for the secondary button, this is less verbose and potentially confusing when validation groups are A) excessive for a single field and B) you have to maintain validation groups when adding further controls (do we put it on the button, the validator, the field and the validation summary? It’s not a lot the remember the standard, but less practical when editing.

Method 4

This is Aspx :

                 <td align="right">
                            Cut Type :
                        </td>
                        <td class="required">
                            <telerik:RadComboBox ID="cmbCutType" runat="server" MaxHeight="200px" Width="200px"
                                Filter="Contains" EnableLoadOnDemand="true" EmptyMessage="Select Cut Type" OnSelectedIndexChanged="cmbCutType_SelectedIndexChanged"
                                AutoPostBack="true">
                            </telerik:RadComboBox>
                            <asp:RequiredFieldValidator runat="server" ID="rfvCutType" Display="None" ControlToValidate="cmbCutType" InitialValue=""
                                ValidationGroup="Save" ErrorMessage="Cut Type is Mandatory"
                                ForeColor="Red"></asp:RequiredFieldValidator>
                            <ajaxToolkit:ValidatorCalloutExtender ID="vceCutType" TargetControlID="rfvCutType"
                                runat="server">
                            </ajaxToolkit:ValidatorCalloutExtender>
                        </td>

This is code behind :

protected void btn2_Click(object sender, EventArgs e)
{
    rfvCutType.IsValid = false;
}

try this…….

Method 5

If you wish you use backend validation then check this out, it worked for me.

Requiredfieldvalidator.Enabled = False

Requiredfieldvalidator is an ID.

The project I am working has all the fields visible but based on certain conditions, validation has to be disabled.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x