我在ASP.NET中创建了一个表单。代码如下所示
<form id="AddSchool" method="post" runat="server">
<span class="failureNotification">
<asp:Literal ID="ErrorMessage" runat="server" ></asp:Literal>
</span>
<asp:ValidationSummary ID="AddSchoolValidationSummary" runat="server" CssClass="failureNotification" ValidationGroup="AddSchoolValidationGroup"/>
<asp:Label ID="SchoolNameLabel" runat="server" Text="School Name " AssociatedControlID="SchoolName"></asp:Label>
<asp:TextBox ID="SchoolName" runat="server" Height="16px" Width="337px" CssClass="textEntry"></asp:TextBox>
<asp:Label ID="Label15" runat="server" Font-Size="Larger" ForeColor="Red" Text="*"></asp:Label>
<asp:RequiredFieldValidator ID="SchoolNameRequired" ControlToValidate="SchoolName" CssClass="failureNotification" runat="server" ErrorMessage="School Name is Required" ToolTip="School Name is Required"
ValidationGroup="AddSchoolValidationGroup"></asp:RequiredFieldValidator>
<asp:Label ID="StreetAddressLabel" runat="server" Text="Street Address " AssociatedControlID="StreetAddress"></asp:Label>
<asp:TextBox ID="StreetAddress" runat="server" Height="16px" Width="338px" CssClass="textEntry"></asp:TextBox>
<asp:Label ID="Label16" runat="server" Font-Size="Larger" ForeColor="Red" Text="*"></asp:Label>
<asp:RequiredFieldValidator ID="StreetAddressRequired" ControlToValidate="StreetAddress" CssClass="failureNotification" runat="server"
ErrorMessage="Street Address is Required" ToolTip="Street Address is Required" ValidationGroup="AddSchoolValidationGroup"></asp:RequiredFieldValidator>
<asp:Label ID="CityLabel" runat="server" Text="City " AssociatedControlID="City" ></asp:Label>
<asp:TextBox ID="City" runat="server" Height="16px" Width="340px" style="margin-left: 0px" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="CityRequired" runat="server" ControlToValidate="City" CssClass="failureNotification"
ErrorMessage="City is Required" ToolTip="City is Required" ValidationGroup="AddSchoolValidationGroup"></asp:RequiredFieldValidator>
<asp:Label ID="StateLabel" runat="server" Text="State " AssociatedControlID="StateList"></asp:Label>
<asp:DropDownList ID="StateList" runat="server" Height="25px" Width="119px" DataSourceID="SqlDataSource1" DataTextField="StateName" DataValueField="StateName" CssClass="textEntry">
<asp:ListItem>Select A State</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label17" runat="server" Font-Size="Larger" ForeColor="Red" Text="*"></asp:Label>
<asp:RequiredFieldValidator ID="StateRequired" runat="server" Display="Dynamic" ControlToValidate="StateList" CssClass="failureNotification"
ErrorMessage="A State is Required" ToolTip="A State is Required" ValidationGroup="AddSchoolValidationGroup"></asp:RequiredFieldValidator>
<asp:Label ID="ZipLabel" runat="server" Text="Zip Code :" AssociatedControlID="ZipCode"></asp:Label>
<asp:TextBox ID="ZipCode" runat="server" Width="139px" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="ZipCodeRequired" runat="server" ControlToValidate="ZipCode" CssClass="failureNotification"
ErrorMessage="Zip Code is Required" ToolTip="Zip Code is Required" ValidationGroup="AddSchoolValidationGroup"></asp:RequiredFieldValidator>
<asp:Label ID="EmailLabel" runat="server" Text="Email Address" AssociatedControlID="Email"></asp:Label>:
<asp:TextBox ID="Email" runat="server" Width="304px" Height="16px" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="EmailRequired" runat="server" ControlToValidate="Email" CssClass="failureNotification"
ErrorMessage="E-mail is required." ToolTip="E-mail is required." ValidationGroup="AddSchoolValidationGroup">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="EmailFormat" runat="server" ControlToValidate="Email" CssClass="failureNotification"
ErrorMessage="The email address entered does not appear to be valid." ValidationGroup="AddSchoolValidationGroup"
ValidationExpression="([A-Z]|[a-z]|[0-9])+[@]([A-Z]|[a-z]|[0-9])+\.([A-Z]|[a-z]){2,6}" Display="Dynamic">*</asp:RegularExpressionValidator>
<asp:Label ID="ConfirmEmailLabel" runat="server" Text="Confirm Email Address " AssociatedControlID="ConfirmEmail"></asp:Label>:
<asp:TextBox ID="ConfirmEmail" runat="server" Height="18px" Width="304px" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="ConfirmEmailRequired" runat="server" ControlToValidate="ConfirmEmail" CssClass="failureNotification"
ErrorMessage="Confirm E-mail is required." ToolTip="Confirm E-mail is required."
ValidationGroup="AddschoolValidationGroup">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="EmailCompare" runat="server" ControlToCompare="Email" ControlToValidate="ConfirmEmail"
CssClass="failureNotification" Display="Dynamic" ErrorMessage="E-mail and Confirm E-mail must match."
ValidationGroup="AddSchoolValidationGroup">*</asp:CompareValidator>
<asp:Label ID="Label14" runat="server" ForeColor="Red" Text="* = Required"></asp:Label>
<asp:ImageButton ID="Cancel" runat="server" OnClick="cancelClick" ImageUrl="~/Styles/images/cancel-btn.png" />
<asp:ImageButton ID="Submit" runat="server" OnClick="submitClick" ImageUrl="~/Styles/images/submit-btn.png" />
</form>
此表单包含一些字段,如学校名称,街道地址,城市,电子邮件,符合电子邮件等。随着Asp.net验证控件和两个图像按钮,一个被命名为取消,另一个被命名为提交。当我点击提交我希望使用该验证控件验证表单并引发一个事件“submitclick”。当我点击取消时我想提出一个事件cancelclick.Can任何人解释如何在我点击提交'图像按钮时验证表单”。
答案 0 :(得分:0)
在ValidationGroup
上添加ImageButton ID="Submit"
:
<asp:ImageButton
ID="Submit"
runat="server"
OnClick="submitClick"
ImageUrl="~/Styles/images/submit-btn.png"
ValidationGroup="AddSchoolValidationGroup" CausesValidation="true"
/>
答案 1 :(得分:0)
在按钮中设置CausesValidation =“True”会让你到达那里。