簡體   English   中英

使用用戶控件時在回發后觸發ASP.Net驗證程序

[英]ASP.Net Validators Fired After Postback when using usercontrols

我在使用時出現一個奇怪的問題:-一個帶有validationgroup =“ vg_save”的用戶控件(包含一組帶有常規asp.net驗證器的文本框)-和一個帶有validationgroup“ 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> 

您需要在驗證器上啟用客戶端驗證

您可以使用驗證器的EnableClientScript屬性

您可以在以下位置找到有關此屬性的信息: http : //msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.basevalidator.enableclientscript(v=vs.110).aspx

當OnClientClick表達式的值為真時,將提交頁面。

在函數中調用Page_ClientValidate,以便在返回true之前檢查頁面的有效性

暫無
暫無

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

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