Question

My search form has 2 fields: Date and Object id. i'm using date validation like this:

<asp:CompareValidator ID="cv" runat="server" Operator="GreaterThanEqual" Type="Date"
                                ControlToValidate="dateControl" ValueToCompare="" Display="None" SetFocusOnError="False"
                                ErrorMessage="error msg" EnableClientScript="True"/>

ValueToCompare is set from code-behind (10 days back from now).

i don't want to run date validation, when Object id field is not empty (allows to search without date restrictions). What are solutions without using CustomValidator?

Was it helpful?

Solution

Simply set the 'Enabled' property to false in Code behind file. The Validation is performed after the Page.Load event but just before the event fires for the button or control that triggered the validation.

// Markup portion

<asp:CompareValidator ID="cv" runat="server" Operator="GreaterThanEqual" Type="Date"
ControlToValidate="dateControl" ValueToCompare="" Display="None" SetFocusOnError="False"
ErrorMessage="error msg" EnableClientScript="True"/>

// Code behind file

protected void Page_Load(object sender, EventArgs e)
    {
        if(!String.IsNullOrEmpty(ObjectId.Text))
         {
             cv.Enabled=false;
         }
    }

Now, when the Validation will be performed, CompareValidator will be skipped. You can also set the 'Visible' property to false as a second option. Check the MSDN here.

OTHER TIPS

Check this.may be this help you:

if(!string.IsNullOrEmpty(Objectid.Text))
 cv.ValueToCompare=DateTime.Now.AddDays(1);
else
 cv.ValueToCompare=DateTime.Now.AddDays(-10);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top