简体   繁体   中英

Why the search button is hitting the validator of AddGuest?

This is default.aspx file used to display first page of website. Now when i hit the search button, even after writing the Redirect code, it isn't redirecting. It only redirects when there is some data in the text box. Why is this happening?

This is the code:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Guest.DAO;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void add_Click(object sender, EventArgs e)
    {
        String guestName = name.Text;
        String guestPhone = phone.Text;

        GuestData guestData = new GuestData();

        guestData.GuestName = guestName;
        guestData.GuestPhone = guestPhone;

        GuestDAL guestDAL = new GuestDAL();
        bool isAdded = guestDAL.AddGuest(guestData);
        if (isAdded)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script ='text'/'javascript'> alert('Guest is Added');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script ='text'/'javascript'> alert('Guest is not Added');</script>");
        }



    }


    protected void search_Click(object sender, EventArgs e)
    {

        Response.Redirect("Search.aspx");   
    }
}

And this is default.aspx.cs:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
   .param{
       width:200px;
       padding:20px;
   }
   .userbackground{
       margin-left:14px;
   }
   .centering{
       width:300px;
   }
   .button_style{
       padding:10px;
   }
   .form_style{
       margin-top:5px;
   }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div class="centering" style="margin-left:auto;margin-right:auto;">

        <div class="form_style">
           <span class="param">Guest Name :</span>
            <asp:TextBox ID="name" runat="server" CssClass="userbackground"></asp:TextBox>
            <asp:RequiredFieldValidator runat="server" ID="reqName" ControlToValidate="name"  errormessage="*"><span style="COLOR: red">*</span></asp:RequiredFieldValidator>
        </div>

        <div class="form_style">
           <span class="param">Guest Number :</span>
            <asp:TextBox ID="phone"  runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator runat="server" Display="Dynamic" ID="RequiredFieldValidator1" ControlToValidate="name"  errormessage="*"><span style="COLOR: red">*</span></asp:RequiredFieldValidator>
        </div>

            <div class="button_style" align="center">
            <asp:Button ID="add" Text="Add Guest" runat="server" OnClick="add_Click" />
            <asp:Button ID="search" Text="Search it" runat="server" OnClick="search_Click"></asp:Button>
            </div>
         </div>

    </form>
</body>
</html>

Without data even if we click the searchButton "*" is being displayed on side of text box.

Use a ValidationGroup with add button and RequiredFieldValidator . Like below

<div class="centering" style="margin-left:auto;margin-right:auto;">

    <div class="form_style">
       <span class="param">Guest Name :</span>
        <asp:TextBox ID="name" runat="server" CssClass="userbackground"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" ID="reqName" ControlToValidate="name" ValidationGroup="a"  errormessage="*"><span style="COLOR: red">*</span></asp:RequiredFieldValidator>
    </div>

    <div class="form_style">
       <span class="param">Guest Number :</span>
        <asp:TextBox ID="phone"  runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator runat="server" Display="Dynamic" ID="RequiredFieldValidator1" ValidationGroup="a" ControlToValidate="name"  errormessage="*"><span style="COLOR: red">*</span></asp:RequiredFieldValidator>
    </div>

        <div class="button_style" align="center">
        <asp:Button ID="add" Text="Add Guest" runat="server" ValidationGroup="a" OnClick="add_Click" />
        <asp:Button ID="search" Text="Search it" runat="server" OnClick="search_Click"></asp:Button>
        </div>
     </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM