문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top