ASP.Net Validators在使用usercontrols后回发后被触发

时间:2014-12-26 19:41:05

标签: c# asp.net user-controls

我使用时出现一个奇怪的问题: - 一个用户控件(包含一组带有常规asp.net验证器的文本框),其中validationgroup =“vg_save” - 以及在此usercontrol外部带有验证组“vg_save”的按钮,这两个按钮都位于asp.net页面的updatepanel中。

当我点击按钮时,它会回发到服务器然后激活验证器。

为什么会回发到服务器

以下是我的.ASPX文件

更新面板包含用户控件和验证用户控件文本框的按钮

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <h4>Add Sponsorship </h4>
            <asp:HiddenField runat="server" ID="hfSponsorshipId" />
            <div id="wizard" style="min-height: 300px">
                <h1>Sponsor Details </h1>
                <div id="dvNewDonor" runat="server" visible="False">
                    <uc1:UC_Donor ID="UC_Donor" runat="server" />
                </div>
                <h1>Payment Details</h1>
                <div>
                    <table>

                        <tr runat="server">
                            <td style="width: 250px">Payment Type
                            </td>
                            <td>
                                <asp:Label runat="server" ID="lblPaymentTypeMark"></asp:Label>
                            </td>
                            <td>
                                <asp:DropDownList runat="server" ID="drpListPaymentTypes">
                                </asp:DropDownList>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator21" runat="server" ControlToValidate="drpListPaymentTypes" InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr runat="server">
                            <td>Amount
                            </td>
                            <td>
                                <asp:Label runat="server" ID="lblAmountMark"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox runat="server" ID="txtPaymentAmount">
                                </asp:TextBox>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator22" runat="server" ControlToValidate="txtPaymentAmount" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr id="Tr4" runat="server">
                            <td>Account Number
                            </td>
                            <td>
                                <asp:Label runat="server" ID="lblAccountNumberMark"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox runat="server" ID="txtAccountNumber">
                                </asp:TextBox>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtAccountNumber" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr id="Tr1" runat="server">
                            <td>Sort Code
                            </td>
                            <td>
                                <asp:Label runat="server" ID="lblSortCodeMark"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox runat="server" ID="txtSortCode">
                                </asp:TextBox>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator23" runat="server" ControlToValidate="txtSortCode" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr id="Tr2" runat="server">
                            <td>Payment Start Date  
                            </td>
                            <td>
                                <asp:Label runat="server" ID="lblPaymentStartDateMark"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox runat="server" ID="txtPaymentStartDate" TextMode="Date">
                                </asp:TextBox>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator24" runat="server" ControlToValidate="txtPaymentStartDate" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr id="Tr3" runat="server">
                            <td>Payment Expiration Date  
                            </td>
                            <td>
                                <asp:Label runat="server" ID="lblPaymentExpirationDateMark"></asp:Label>
                            </td>
                            <td>
                                <asp:TextBox runat="server" ID="txtPaymentExpirationDate" TextMode="Date">
                                </asp:TextBox>
                            </td>
                            <td>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator25" runat="server" ControlToValidate="txtPaymentExpirationDate" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td></td>
                            <td>
                                <asp:Button ID="btnAddSponsorship" runat="server" Text="Save" ValidationGroup="vgSave" Width="99px" OnClientClick=" return ss();" OnClick="btnAddSponsorship_Click" />
                            </td>
                        </tr>

                        <tr>
                            <td></td>
                            <td></td>
                            <td colspan="2">
                                <asp:Label runat="server" ID="lblMsg" Style="font-weight: bold; font-size: 12pt; padding: 5px"></asp:Label>

                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>

**这是我的.ASCX文件

一些带有asp.net验证的文本框**

<table>
    <tr>
        <td style="width: 250px">
            Main Donor Type
        </td>
        <td>
            <asp:Label runat="server" ID="lblMainDonorTypeMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" AutoPostBack="True" ID="listDonorIs" OnSelectedIndexChanged="listDonorIs_SelectedIndexChanged">
                <asp:ListItem Value="1">Individual</asp:ListItem>
                <asp:ListItem Value="2">Organisation</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
    <tr runat="server" id="trTitles">
        <td>
            Title
        </td>
        <td>
            <asp:Label runat="server" ID="lblTitleMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListTitles">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator15" EnableClientScript="False"
                runat="server" ControlToValidate="drpListTitles" InitialValue="-1" Text="*" SetFocusOnError="True"
                ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trFirstName">
        <td>
            First Name
        </td>
        <td>
            <asp:Label runat="server" ID="lblFirstNameMark"></asp:Label>
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtFirstName">
            </asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator16" EnableClientScript="False"
                runat="server" ControlToValidate="txtFirstName" Text="*" SetFocusOnError="True"
                ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trLastName">
        <td>
            Last Name
        </td>
        <td>
            <asp:Label runat="server" ID="lblLastNameMark"></asp:Label>
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtLastName">
            </asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server" ControlToValidate="txtLastName"
                Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr id="trOrgName" runat="server" visible="False">
        <td>
            Organisation Name
        </td>
        <td>
            <asp:Label runat="server" ID="lblDonorMark"></asp:Label>
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtDonorName"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtDonorName"
                Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trJob">
        <td>
            Job
        </td>
        <td>
            <asp:Label runat="server" ID="lblJobMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListJobs">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="drpListJobs"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trJobPosition">
        <td>
            Job Position
        </td>
        <td>
            <asp:Label runat="server" ID="lblJobPositionMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListJobPositions">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="drpListJobPositions"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trGender">
        <td>
            Gender
        </td>
        <td>
            <asp:Label runat="server" ID="lblGenderMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListGender">
                <asp:ListItem Value="1">Male</asp:ListItem>
                <asp:ListItem Value="2">Female</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td>
            Donor Type
        </td>
        <td>
            <asp:Label runat="server" ID="lblDonorTypeMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListDonorType">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="drpListDonorType"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trBirthDate">
        <td>
            Birth Date
        </td>
        <td>
            <asp:Label runat="server" ID="lblBirthDateMark"></asp:Label>
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtBirthDate" TextMode="Date"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ControlToValidate="txtBirthDate"
                Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trNationality">
        <td>
            Nationality
        </td>
        <td>
            <asp:Label runat="server" ID="lblNationalityMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListNationality">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="drpListNationality"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trOrgType" visible="False">
        <td>
            Organisation Type
        </td>
        <td>
            <asp:Label runat="server" ID="lblOrgTypeMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListOrganizationType">
            </asp:DropDownList>
            <td>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ControlToValidate="drpListOrganizationType"
                    InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
            </td>
        </td>
    </tr>
    <tr runat="server" id="trIndustry" visible="False">
        <td>
            Industry
        </td>
        <td>
            <asp:Label runat="server" ID="lblIndustryMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListIndustries">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" ControlToValidate="drpListIndustries"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trCoOperationField" visible="False">
        <td>
            Co-Operation Field
        </td>
        <td>
            <asp:Label runat="server" ID="lblCooperationFieldMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListCooperationFields">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" ControlToValidate="drpListCooperationFields"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr runat="server" id="trCommunicationRepresentative" visible="False">
        <td>
            Communication Representative
        </td>
        <td>
            <asp:Label runat="server" ID="lblRepMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListCommunicationRepresentative">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ControlToValidate="drpListCommunicationRepresentative"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            Email Address
        </td>
        <td>
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtEmail" TextMode="Email"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="txtEmail"
                Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgEmail"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Button runat="server" ID="btnAddEmail" ValidationGroup="vgEmail" Text="+" Width="30px"
                OnClick="btnAddEmail_Click" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
            <asp:ListBox runat="server" ID="listMails" Height="75px"></asp:ListBox>
        </td>
        <td>
        </td>
        <td>
            <asp:Button runat="server" ID="btnRemoveMail" Text="-" Width="30px" OnClick="btnRemoveMail_Click" />
        </td>
    </tr>
    <tr>
        <td>
            Telephone Numbers
        </td>
    </tr>
    <tr>
        <td style="color: blue">
            &nbsp;&nbsp;&nbsp;&nbsp;Home
        </td>
        <td>
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtHomePhone"></asp:TextBox>
        </td>
        <td>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td style="color: blue">
            &nbsp;&nbsp;&nbsp;&nbsp;Business
        </td>
        <td>
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtBusinessPhone"></asp:TextBox>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td style="color: blue">
            &nbsp;&nbsp;&nbsp;&nbsp;Mobile
        </td>
        <td>
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtMobilePhone"></asp:TextBox>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td>
            Address
        </td>
        <td>
        </td>
        <td>
            <asp:TextBox runat="server" TextMode="MultiLine" ID="txtAddress"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator14" runat="server" ControlToValidate="txtAddress"
                Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgAddress"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Button runat="server" ID="btnAddAddress" ValidationGroup="vgAddress" Text="+"
                Width="30px" OnClick="btnAddAddress_Click" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
            <asp:ListBox runat="server" ID="listAddresses" Height="75px"></asp:ListBox>
        </td>
        <td>
        </td>
        <td>
            <asp:Button runat="server" ID="btnRemoveAddress" Text="-" Width="30px" OnClick="btnRemoveAddress_Click" />
        </td>
    </tr>
    <tr>
        <td>
            Prefered Contact Method
        </td>
        <td>
            <asp:Label runat="server" ID="lblPreferedMethodMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListContactMethods">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" ControlToValidate="drpListContactMethods"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgContactMthod"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Button runat="server" ID="btnAddContactMethod" ValidationGroup="vgContactMthod"
                Text="+" Width="30px" OnClick="btnAddContactMethod_Click" />
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
            <asp:ListBox runat="server" ID="lstSelectedContactMethods" Height="75px"></asp:ListBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" ControlToValidate="lstSelectedContactMethods"
                Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
        <td>
            <asp:Button runat="server" ID="btnRemoveContactMethod" Text="-" Width="30px" OnClick="btnRemoveContactMethod_Click" />
        </td>
    </tr>
    <tr>
        <td>
            Tax Payer
        </td>
        <td>
            <asp:Label runat="server" ID="lblTaxPayerMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListTaxPayer" AutoPostBack="True" OnSelectedIndexChanged="drpListTaxPayer_SelectedIndexChanged">
                <asp:ListItem Value="2">No</asp:ListItem>
                <asp:ListItem Value="1">Yes</asp:ListItem>
            </asp:DropDownList>
        </td>
    </tr>
    <tr id="trGiftAidStartDate" runat="server" visible="False">
        <td>
            Gift Aid Start date
        </td>
        <td>
            <asp:Label runat="server" ID="lblGiftAidStartDateMark"></asp:Label>
        </td>
        <td>
            <asp:TextBox runat="server" ID="txtGiftAidStartDate" TextMode="Date"></asp:TextBox>
        </td>
    </tr>
    <tr id="trGiftAidDeclarationType" runat="server" visible="False">
        <td>
            Gift Aid Declaration Type
        </td>
        <td>
            <asp:Label runat="server" ID="lblGiftAidDeclarationTypeMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListGiftAidDeclarationType" />
        </td>
    </tr>
    <tr>
        <td>
            Donor Status
        </td>
        <td>
            <asp:Label runat="server" ID="lblDonorStatusMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListDonorStatus">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator18" runat="server" ControlToValidate="drpListClassifications"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            Donor Classifcation
        </td>
        <td>
            <asp:Label runat="server" ID="lblClassTypeMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListClassifications">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator19" runat="server" ControlToValidate="drpListClassifications"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            Donation confirmation Method
        </td>
        <td>
            <asp:Label runat="server" ID="lblConfirmationMethodsMark"></asp:Label>
        </td>
        <td>
            <asp:DropDownList runat="server" ID="drpListConfirmationMethods">
            </asp:DropDownList>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator20" runat="server" ControlToValidate="drpListClassifications"
                InitialValue="-1" Text="*" SetFocusOnError="True" ForeColor="red" ValidationGroup="vgSave"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td colspan="2">
            <asp:Label runat="server" ID="lblMsg" Style="font-weight: bold; font-size: 12pt;
                padding: 5px"></asp:Label>
        </td>
    </tr>
</table>

2 个答案:

答案 0 :(得分:1)

您需要在验证程序上启用客户端验证

您可以使用验证程序的EnableClientScript属性

您可以在此处找到有关此属性的信息: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.enableclientscript(v=vs.110).aspx

答案 1 :(得分:1)

当OnClientClick表达式的计算结果为true时,页面将被提交。

在您的函数中调用Page_ClientValidate,以便在返回true之前检查页面的验证