簡體   English   中英

CompareValidator無法正常工作

[英]CompareValidator not working correctly

我寧願使用asp.net驗證控件,因為我目前在同一視圖中還有其他驗證控件。 我需要在驗證摘要中顯示錯誤消息。

我有兩個文本框,我需要確保文本框A比文本框B少。

我已經使用CompareValidator並將屬性設置為:

  • ControlToCompare:文本框B
  • ControlToValidate:textboxA
  • 接線員:GreaterThan /還嘗試了LessThan
  • 類型:日期

這是問題所在:

  • 當我在textboxA中提供時間並移至textboxB時,將顯示驗證錯誤。 我以為我的if語句可以解決此問題,但不能解決。

在“更新”按鈕的Click事件中,我添加了以下代碼,因為我只需要它來驗證textboxA / textboxB!= null。

        if(String.IsNullOrEmpty(textboxB.Text))
        {
            Debug.Write("Valid");
            timeCompareValidator.IsValid = true;    
        }

在此先感謝您的幫助。

克萊爾

我認為您需要使用CustomValidator,並且仍將允許您使用ValidationSummary。

看這里:

如何在沒有日期的時間中使用CompareValidator?

如果我對您的理解正確,則需要比較兩個日期。

我在msdn上找到了這段代碼

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";

Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
// The example displays the following output:
//    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM

您是否要嘗試將if語句更改為:

if (!string.IsNullOrEmpty(textboxA.Text) && !string.IsNullOrEmpty(textboxB.text))

如果要在服務器端比較兩個日期或時間,請使用此解決方案

DateTime dt1 = Convert.ToDateTime(TextBoxA.Text);
DateTime dt2 = Convert.ToDateTime(TextBoxB.Text);

int result = dt1.CompareTo(dt2)

我將使用CustomValidator( http://msdn.microsoft.com/zh-cn/library/9eee01cx ( VS.71 ) .aspx )。 順便說一句:您為什么直接致電驗證者?

timeCompareValidator.Validate();

通常,在每個Button觸發的Page.Validate()過程中評估驗證器(如果CausesValidation未設置為false)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM