2010-03-11 99 views
0

我创建了一个自定义asp.net控件的一些字段有验证控制与他们相关联。 当我在页面上声明多个初始化时,就会出现问题。当我点击一个初始化控件时,表单验证发生在所有已声明的其他控件上。由于这个问题,我无法提交表格。这里是代码asp.net自定义控件与表单字段验证问题

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="FinancialAdvisorHelp.ascx.cs" Inherits="FinancialAdvisorHelp" %> 

<table width="316" border="0" cellpadding="0" cellspacing="0"> 
    <tr> 
    <td colspan="2" > 
    <asp:RequiredFieldValidator 
       ID="RequiredFieldValidatorMember" runat="server" 
       ErrorMessage="Pleae enter member name. " ControlToValidate="TextBox_Name"></asp:RequiredFieldValidator> 

     <asp:RequiredFieldValidator ID="RequiredFieldValidatorEmail" runat="server" ErrorMessage="Please enter email. " ControlToValidate="TextBox_email"></asp:RequiredFieldValidator> 
     <asp:RegularExpressionValidator ID="RegularExpressionValidatorDarryEmail" runat="server" 
        ControlToValidate="TextBox_email" ValidationExpression=".*@.*\..*" 
        ErrorMessage="<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Invalid Email."> </asp:RegularExpressionValidator> 
    </td> 
    </tr> 
    <tr> 
    <td width="165"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding:0;">Member Name*</p> 
     <asp:TextBox ID="TextBox_Name" runat="server" CausesValidation="True"></asp:TextBox> 

     </td> 
    <td width="151" align="right"><p style="font-family:Palatino Linotype; font-size:12px; margin:0 0 0 4px; padding:0; text-align:left;">E-Mail Address*</p> 
     <asp:TextBox id="TextBox_email" runat="server" CausesValidation="True"></asp:TextBox> 

     </td> 
    </tr> 
    <tr> 
    <td colspan="2" align="right"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding-right:50px;"><br /> 
     Telephone Number</p> 
    <asp:TextBox id="TextBox_phone" runat="server" CausesValidation="True"></asp:TextBox> 
    </td> 
    </tr> 
    <tr> 
    <td colspan="2"><p style="font-family:Palatino Linotype; font-size:12px; margin:0; padding:0;">Ask Darryl your question</p> 
     <asp:TextBox ID="TextBox_question" runat="server" Rows="7" Columns="48" 
      style="width:310px" TextMode="MultiLine"></asp:TextBox> 
     </td> 
    </tr> 
    <tr> 
    <td colspan="2"><p style="font-size:10px; margin:0; padding:0; float:left; width:240px; line-height:12px;">Note: you should receive a response within two (2) business days.<br /> 
    *Required information.</p> 
     <asp:ImageButton ID="ImageButton_Advisor" runat="server" alt="submit" 
      width="63" height="18" 
      style="border:0; padding:0; margin:10px 0 0 0; float:right;" ImageUrl="./images/investments/submit.gif" 
      /> 
      <asp:HiddenField ID="HiddenFieldAdvisorEmail" Value="" runat="server" /> 
     </td> 
     <tr><td colspan="2"> 
      &nbsp;</td></tr> 
    </tr> 
</table> 

回答

0

我很难破译你的问题。

  • 确认团组

这听起来像你可能要采取的validation groups优势。当你有多个触发器应该验证不同的控件时,这就是要使用的。

  • 检查校验和自定义控件

如果您仍然有问题,当验证有错误,你触发验证,看来“什么也没发生”(真正的情况是,验证器错误,页面类型停止)。您的自定义控件也可能存在问题。

  • 检查校验和ID的

问题arised当我宣布更多 比一个初始化页面

你是什么意思呢?我不知道你是否在代码中动态地声明控件?这也可能是你的问题。每个ASP.NET控件(包括验证器,自定义控件,...)都需要它自己的唯一ID。您也可以有多个验证器检查单个控件,但单个验证器无法验证多个控件。

+0

我多次调用该控制 单页上例如 在页面中。 当我尝试提交控制之一。验证也在第二个控件上执行。 如果第一个控件实例中的所有字段都是有效的,那么它应该提交,但是由于第二个控件实例的验证失败,它不能提交。那是不可能的。 – 2010-03-11 20:57:44

+0

哦,所以要提交页面,第一个控件必须是有效的,但是第二个控件无效是可以的?如果是这样,请将验证控件关闭,或者查看是否可以将CausesValidation = False添加到第二个自定义控件。 – 2010-03-11 21:40:43

+0

我觉得这听起来更像是你有一个情况,用户必须填写第一个控件,然后如果他们想要,他们可以填写第二个,(第三,第四,..?)。考虑一个简单的重新设计,其中第二个(/下一个)控件是不可见的,直到他们表明他们想要填写它。不可见(即Visible =“False”)标准ASP.NET控件未经过验证(不确定您的自定义)。 – 2010-03-11 21:44:43

1

对导致验证的所有表单元素(包括提交按钮)使用ValidationGroup属性。当触发验证时,它将仅检查具有指定的相同ValidationGroup的其他元素。

要确保它在页面上的每个控件实例中都是唯一的,请在您的代码后面使用UserControl的ID作为验证组。

RequiredFieldValidatorEmail.ValidationGroup = this.ClientID; 

的更多信息可在这里找到:http://www.dotnet-guide.com/validationgroups.html

+0

在单个页面上多次调用此控件,例如在页面中。当我试图提交控制之一。验证也在第二个控件上执行。如果控制的第一个实例中的所有字段都是有效的,那么它应该提交,但是由于第二个控制实例的验证失败而不能提交。那是不可能的 – 2010-03-11 20:58:23