Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么搜索按钮会点击AddGuest的验证程序?_C#_Asp.net_.net - Fatal编程技术网

C# 为什么搜索按钮会点击AddGuest的验证程序?

C# 为什么搜索按钮会点击AddGuest的验证程序?,c#,asp.net,.net,C#,Asp.net,.net,这是用于显示网站首页的default.aspx文件。现在,当我点击搜索按钮时,即使在编写重定向代码之后,它也不是重定向。 它仅在文本框中有一些数据时重定向。为什么会这样 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Guest.DAO;

这是用于显示网站首页的default.aspx文件。现在,当我点击搜索按钮时,即使在编写重定向代码之后,它也不是重定向。 它仅在文本框中有一些数据时重定向。为什么会这样

代码如下:

    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");   
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用Guest.DAO;
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效添加\单击(对象发送方,事件参数e)
{
字符串guestName=name.Text;
字符串guestPhone=phone.Text;
GuestData GuestData=新GuestData();
guestData.GuestName=GuestName;
guestData.GuestPhone=GuestPhone;
GuestDAL GuestDAL=新GuestDAL();
bool isAdded=guestDAL.AddGuest(guestData);
如果(已添加)
{
RegisterStartupScript(this.GetType(),“”,“,”警报('Guest is Added');”;
}
其他的
{
RegisterStartupScript(this.GetType(),“”,“,”警报('未添加来宾');”;
}
}
受保护的无效搜索\u单击(对象发送者,事件参数e)
{
重定向(“Search.aspx”);
}
}
这是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>

帕拉姆先生{
宽度:200px;
填充:20px;
}
.userbackground{
左边距:14px;
}
.定心{
宽度:300px;
}
.按钮式{
填充:10px;
}
.form_风格{
边缘顶部:5px;
}
客人姓名:
*
客人号码:
*

没有数据,即使我们单击文本框一侧的搜索按钮“*”。

使用带有
添加
按钮的
验证组
RequiredFieldValidator
。如下

<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>

客人姓名:
*
客人号码:
*

验证组
添加
按钮
所需字段验证程序
一起使用。如下

<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>

客人姓名:
*
客人号码:
*