开发者

Problem in using RequiredFieldValidator control in asp.net c#

开发者 https://www.devze.com 2023-02-27 00:04 出处:网络
I am using RequiredFieldValidator in my asp.net program. I have a sign out button which is used to terminate the session, but when i click on this sign out button without inserting any values in any o

I am using RequiredFieldValidator in my asp.net program. I have a sign out button which is used to terminate the session, but when i click on this sign out button without inserting any values in any of the text box the RequiredFieldValidator throws an error and i am not able to sign out.

I want to sign out a user without inputting any of the values in the textbox.

Please check my code in case of any problem in my code.

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 code

<%@ 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"></as开发者_JAVA技巧p: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>


There are 2 ways you can do this

put validation group property to your controls

ValidationGroup="input"

OR

On your sign out link button, put

CausesValidation="false"


Use validation group property to group the items to be validated.
http://www.w3schools.com/ASPNET/prop_webcontrol_imagebutton_validationgroup.asp


On your Sign Out button set CausesValidation property to false

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


There is a property of button CausesValidation; set it to false. Than the validations on page won't fire.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号