Enable/Disable Required field validator from cs page?
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
}
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.
use the ValidationGroup="group"
property to button
and assign validation group to text on which you want to validate.
Hope it will help