在asp.net c#中使用RequiredFieldValidator控件的问题
本文关键字:RequiredFieldValidator 控件 问题 asp net | 更新日期: 2023-09-27 17:50:43
我在我的asp.net程序中使用RequiredFieldValidator。我有一个签出按钮,用于终止会话,但是当我点击这个签出按钮而不插入任何值在任何文本框RequiredFieldValidator抛出一个错误,我不能签出。
我想在不输入文本框中的任何值的情况下注销用户。
如果我的代码有问题,请检查我的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class EntryForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{// CHEK SESSION VARIABLE AND LOAD dropdownlist1 WITH VALUES
if (!IsPostBack)
{
String DB = "";
String AccountID = "";
if (Session["login"] != null && Session["db"] != null)
{
AccountID = Session["login"].ToString();
DB = Session["db"].ToString();
Label9.Text = AccountID;
}
else
{
Response.Redirect("log.aspx");
}
HiddenField1.Value = DB.ToString();
DropDown a = new DropDown();
a.filldropdown1(this.DropDownList1, DB);
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
// LOG OUT***********////////////
Session.Abandon();
Response.Redirect("log.aspx");
}
。aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EntryForm.aspx.cs" Inherits="EntryForm" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit.HTMLEditor" tagprefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 330px;
}
.style2
{
text-align: center;
}
.style3
{
text-align: center;
width: 38px;
}
.style4
{
text-align: center;
width: 109%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table style="width:90%; height: 30px;">
<tr>
<td class="style1">
<asp:Label ID="Label8" runat="server" style="text-align: left"
Text="Welcome"></asp:Label>
<asp:Label ID="Label9" runat="server" style="text-align: left"></asp:Label>
</td>
<td align="center" width="100%" style="text-align: right">
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"
style="text-align: right">Log Out</asp:LinkButton>
</td>
</tr>
</table>
</div>
<table style="width:95%;" align="center" bgcolor="Silver">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Type : "></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="DropDownList1" ErrorMessage="*"
InitialValue="<-- Select -->">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text="No. :"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="75px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="*">*</asp:RequiredFieldValidator>
</td>
<td>
<asp:Label ID="Label3" runat="server" Text="Year :"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="75px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Enter Year">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Enter year"
ValidationExpression="^'d{4}$">YYYY</asp:RegularExpressionValidator>
</td>
<td>
<asp:Label ID="Label4" runat="server" Text="Order Date : "></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Width="75px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Enter proper format">*</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Enter valid date"
ValidationExpression="^(((0[1-9]|[12]'d|3[01])'/(0[13578]|1[02])'/((19|[2-9]'d)'d{2}))|((0[1-9]|[12]'d|30)'/(0[13456789]|1[012])'/((19|[2-9]'d)'d{2}))|((0[1-9]|1'd|2[0-8])'/02'/((19|[2-9]'d)'d{2}))|(29'/02'/((1[6-9]|[2-9]'d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$">dd/mm/yyyy</asp:RegularExpressionValidator>
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="GO" onclick="Button2_Click" />
</td>
</tr>
</table>
<br />
<table style="width:100%;">
<tr>
<td style="text-align: center">
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"
style="text-align: center" Visible="False"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
</table>
<br />
<table style="width:100%;">
<tr>
<td style="text-align: center" width="100%">
<asp:Label ID="Label5" runat="server" Text="Label" Visible="False"
style="text-align: center"></asp:Label>
</td>
<td style="text-align: center" width="100%">
<asp:Label ID="Label6" runat="server" Text="Vs" Visible="False"></asp:Label>
</td>
<td style="text-align: center" width="100%">
<asp:Label ID="Label7" runat="server" Text="Label" Visible="False"></asp:Label>
</td>
</tr>
</table>
<br />
<table style="width:100%;" border="1">
<tr>
<td class="style3" width="100%">
</td>
<td class="style4" width="100%">
<cc1:Editor ID="Editor1" runat="server" Width="60%" AutoFocus="true"
style="text-align: right" />
</td>
<td class="style2" width="100%">
</td>
</tr>
</table>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:HiddenField ID="HiddenField1" runat="server"/>
</form>
</body>
</html>
有两种方法可以做到
将验证组属性放入控件
ValidationGroup="input"
或
在您的sign out链接按钮上,放置
CausesValidation="false"
使用validation group属性对要验证的项进行分组。
http://www.w3schools.com/ASPNET/prop_webcontrol_imagebutton_validationgroup.asp
在Sign Out按钮上设置CausesValidation属性为false
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click"
CausesValidation="False"
style="text-align: right">Log Out</asp:LinkButton>
按钮CausesValidation
;将其设置为false。