[英]Why the search button is hitting the validator of AddGuest?
這是用於顯示網站首頁的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");
}
}
這是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>
沒有數據,即使我們單擊搜索按鈕“ *”也顯示在文本框的側面。
將ValidationGroup
與add
button
和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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.