2011-04-20 20 views
0

我在我的asp.net程序中使用RequiredFieldValidator。我有一個退出按鈕,用於終止會話,但是當我點擊這個退出按鈕而沒有在任何文本框中插入任何值時,RequiredFieldValidator會引發錯誤,並且我無法退出。在asp.net c中使用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"> 
       &nbsp;<asp:Label ID="Label8" runat="server" style="text-align: left" 
        Text="Welcome"></asp:Label> 
&nbsp;<asp:Label ID="Label9" runat="server" style="text-align: left"></asp:Label> 
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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> 
       &nbsp;&nbsp;&nbsp;&nbsp; 
      </td> 
     </tr> 
    </table> 
    </div> 
    <table style="width:95%;" align="center" bgcolor="Silver"> 
     <tr> 
      <td> 
       <asp:Label ID="Label1" runat="server" Text="Type : "></asp:Label> 
&nbsp; 
       <asp:DropDownList ID="DropDownList1" runat="server"> 
       </asp:DropDownList> 
       <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
        ControlToValidate="DropDownList1" ErrorMessage="*" 
        InitialValue="&lt;-- Select --&gt;">*</asp:RequiredFieldValidator> 
      </td> 
      <td> 
       <asp:Label ID="Label2" runat="server" Text="No. :"></asp:Label> 
&nbsp;<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> 
&nbsp; 
       <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%"> 
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </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%"> 
       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td> 
     </tr> 
    </table> 
    <br /> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
          </asp:ScriptManager> 
    <asp:HiddenField ID="HiddenField1" runat="server"/> 
    </form> 
    </body> 
</html> 

回答

1

在您退出按鈕設置CausesValidation屬性設置爲false

<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click" 
        CausesValidation="False" 
        style="text-align: right">Log Out</asp:LinkButton> 
3

有2種方法可以做到這一點

放驗證組屬性你的控件

ValidationGroup="input" 

OR

在您登出鏈接按鈕,放

CausesValidation="false" 
1

有一個按鈕CausesValidation的財產;將其設置爲false。比頁面上的驗證不會觸發。

相關問題