简体   繁体   English

为什么搜索按钮点击了AddGuest的验证器?

[英]Why the search button is hitting the validator of AddGuest?

This is default.aspx file used to display first page of website. 这是用于显示网站首页的default.aspx文件。 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: 这是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 . ValidationGroupadd buttonRequiredFieldValidator 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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