我正在填写注册表格,并且有一个清除按钮。表单具有对所有文本框的验证,以确保它们是正确的。
我有一个清除按钮,它可以工作,但是只有当验证是正确的(这不是我想要的)时,我才想清除它,有什么办法吗?
这是我用于清除按钮的事件处理程序的代码
protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
TextBoxEmail.Text = String.Empty;
TextBoxEmail2.Text = String.Empty;
TextBoxPassword1.Text = String.Empty;
TextBoxPassword2.Text = String.Empty;
TextBoxUsername.Text = String.Empty;
TextBoxWebsite.Text = String.Empty;
}
这是该网页的其他代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
Inherits="Project5.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sign Up Page</title>
<link href="StyleSheet1.css" rel="stylesheet" type="text/css" />
<link href="StyleSheet1.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style3 {
width: 402px;
text-align: left;
}
.auto-style4 {
margin-left: 0px;
}
.auto-style6 {
font-family: "comic Sans MS";
color: #3366FF;
}
.validator {
font-size: small;
color: red;
}
.auto-style7 {
width: 327px;
text-align: center;
}
.auto-style8 {
text-align: right;
}
.auto-style9 {
width: 402px;
text-align: center;
}
</style>
<h1>
<img src="Images/scccLogo.png" />
</h1>
<h2>
Please register you SCCC account
</h2>
</head>
<body>
<form id="form1" runat="server">
<div class="auto-style8">
<div>
</div>
<table class="auto-style1">
<tr>
<td class="auto-style7">
<asp:Label ID="LabelUsrName" runat="server" Text="Please
Enter Your Username:" CssClass="auto-style6"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="TextBoxUsername" runat="server"
Width="185px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="TextBoxUsername" CssClass="validator"
ErrorMessage="* Please Enter A Username"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style7">
<asp:Label ID="LabelPassword" runat="server"
Text="Please Enter Your Password:" CssClass="auto-style6"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="TextBoxPassword1" runat="server"
Width="185px" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2"
runat="server" ControlToValidate="TextBoxPassword1" CssClass="validator"
ErrorMessage="* Please Enter A Password"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style7">
<asp:Label ID="LabelPasswordRepeat" runat="server"
Text="Please Enter Your Password Again:" CssClass="auto-style6"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="TextBoxPassword2" runat="server"
Width="185px" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3"
runat="server" ControlToValidate="TextBoxPassword2" CssClass="validator"
ErrorMessage="* Please Enter A Password"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator1"
runat="server" ControlToCompare="TextBoxPassword1"
ControlToValidate="TextBoxPassword2" CssClass="validator" ErrorMessage="*
Passwords Do Not Match"></asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style7">
<asp:Label ID="LabelEmail" runat="server" Text="Please
Enter A Valid Email Address:" CssClass="auto-style6"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="TextBoxEmail" runat="server"
Width="185px" TextMode="Email"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4"
runat="server" ControlToValidate="TextBoxEmail" CssClass="validator"
ErrorMessage="* Please Enter An Email"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator2" runat="server"
ControlToValidate="TextBoxEmail" CssClass="validator" ErrorMessage="* Please
Enter A Valid Email" ValidationExpression="\w+([-+.']\w+)*@\w+
([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style7">
<asp:Label ID="LabelEmail2" runat="server" Text="Please
Enter Your Email Again:" CssClass="auto-style6"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="TextBoxEmail2" runat="server"
Width="185px" TextMode="Email"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5"
runat="server" ControlToValidate="TextBoxEmail2" CssClass="validator"
ErrorMessage="* Please Enter An Email"></asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator2"
runat="server" ControlToCompare="TextBoxEmail"
ControlToValidate="TextBoxEmail2" CssClass="validator" ErrorMessage="*
Emails Do Not Match"></asp:CompareValidator>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator3" runat="server"
ControlToValidate="TextBoxEmail2" CssClass="validator" ErrorMessage="*
Please Enter A Valid Email" ValidationExpression="\w+([-+.']\w+)*@\w+
([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style7">
<asp:Label ID="LabelWebsite" runat="server" Text="Please
Enter Your Website" CssClass="auto-style6"></asp:Label>
</td>
<td class="auto-style3">
<asp:TextBox ID="TextBoxWebsite" runat="server"
CssClass="auto-style4" Width="185px" TextMode="Url"></asp:TextBox>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBoxWebsite" CssClass="validator" ErrorMessage="*
Please Enter A Valid URL" ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+
(/[\w- ./?%&=]*)?"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style7">
<asp:Label ID="Label1" runat="server"
CssClass="validator" Text="* Required Field"></asp:Label>
</td>
<td class="auto-style9">
<asp:ImageButton ID="ImageButtonClear" runat="server"
CssClass="auto-style4" Height="105px" ImageUrl="~/Images/32293367-red-clear-
button.jpg" OnClick="ImageButton2_Click" Width="112px" />
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/Images/go.jpg" OnClick="ImageButton1_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
您的ImageButton导致验证,这是默认设置。我认为您只需要像这样添加CausesValidation =“ false”属性即可:
<asp:ImageButton ID="ImageButtonClear" runat="server"
CssClass="auto-style4" Height="105px" ImageUrl="~/Images/32293367-red-clear-
button.jpg" OnClick="ImageButton2_Click" CausesValidation="false" Width="112px" />