ASP.Net 使用用户控件时回发后触发的验证程序

本文关键字:程序 验证 Net 用户 控件 ASP | 更新日期: 2023-09-27 18:35:25

我在使用时出现一个奇怪的问题:- 一个用户控件(包含一组带有常规 asp.net 验证程序的文本框),其中 ValidationGroup="vg_save"- 以及此用户控件外部带有验证组"vg_save"的按钮,并且两者都位于 asp.net 页面的更新面板中。

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

为什么会发生到服务器的回发

这是我的.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>

ASP.Net 使用用户控件时回发后触发的验证程序

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

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

您需要

在验证器上启用客户端验证

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

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