自定义验证器未触发

时间:2015-08-04 19:56:52

标签: c# asp.net validation datetime customvalidator

我有一个详细信息视图,我用它来将数据插入数据库但是我有两个文本框,日期[________________]和完成日期[______________] 现在我使用自定义验证器来验证完成日期,说完成日期不能<用户输入的日期。

这是我的完成日期.aspx

    <asp:TemplateField HeaderText="CompletionDate" SortExpression="CompletionDate">
  <EditItemTemplate>
      <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("CompletionDate") %>'></asp:TextBox>
  </EditItemTemplate>
  <InsertItemTemplate>
     <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CompletionDate") %>' 
      Height="29px" Width="599px" CausesValidation="True"></asp:TextBox>
      <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Completion Date Field Required" ControlToValidate = "TextBox2"></asp:RequiredFieldValidator>
     <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="Date format in DD/MM/YYYY" ValidationExpression = "^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[-/.](19|20)\d\d$" ControlToValidate = "TextBox2"></asp:RegularExpressionValidator>
    <asp:CustomValidator ID="CustomValidator" runat="server" ErrorMessage="Completion Date Cannot Be In The Past"  ControlToValidat="TextBox2" OnServerValidate="cusCustom_ServerValidate" EnableClientScript="true" ClientValidationFunction="ValidateCompletionDate" Display = "Dynamic" ValidateEmptyText="True"></asp:CustomValidator>
  </InsertItemTemplate>
    <ItemTemplate>
      <asp:Label ID="Label3" runat="server" Text='<%# Bind("CompletionDate") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

这是代码背后的代码:

 public void cusCustom_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
    {
       DateTime TextBoxDateValid =    Convert.ToDateTime(DetailsView1.FindControl("TextBox1"));
        DateTime TextBoxCompletionDateValid =  Convert.ToDateTime(DetailsView1.FindControl("TextBox2"));
        // args.IsValid = true;

        if (TextBoxDateValid < TextBoxCompletionDateValid)
        {
          //CustomValidator CustomValidator1 = new CustomValidator();
          // CustomValidator1.ErrorMessage = "Completion Date Cannot be in the past";
            args.IsValid = false;

        }
         else
        {
         args.IsValid = true;
         }

      }

我做了很多在线阅读并尝试了所有建议,但找不到任何有用的信息

public void cusCustom_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)

只是没有开火!!!!!!

提前感谢所有回复

当我离开完成日期文本框我希望偶数发射并验证时,我想要的是正确的。

我尝试了Code的解决方案,但它无法正常工作

<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CompletionDate") %>' 
   Height="29px" Width="599px" 
   ontextchanged="cusCustom_ServerValidateit"></asp:TextBox>



public void cusCustom_ServerValidateit()
  {
    DateTime TextBoxDateValid = Convert.ToDateTime(DetailsView1.FindControl("TextBox1"));
    DateTime TextBoxCompletionDateValid = Convert.ToDateTime(DetailsView1.FindControl("TextBox2"));
    // args.IsValid = true;

    if (TextBoxDateValid < TextBoxCompletionDateValid)
       {
        //CustomValidator CustomValidator1 = new CustomValidator();
       // CustomValidator1.ErrorMessage = "Completion Date Cannot be in the past";
       TextBox lab = (TextBox)DetailsView1.FindControl("TextBox12");
       lab.Text = "Completion Date  Cannot be in the past";
       lab.Visible = true;
    }
       else
   {
   TextBox lab = (TextBox)DetailsView1.FindControl("TextBox12");
   lab.Text = "Completion Date  Cannot be in the past";
   lab.Visible = false; 
        }

    }

编译器错误消息:CS0123:'cusCustom_ServerValidateit'没有重载匹配委托'System.EventHandler'

0 个答案:

没有答案