看看下面的代碼。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox ID="txtEmail" runat="server" Text='<%# Eval("Email") %>' Width="88px"
CausesValidation="True" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter a valid email id!"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtEmail" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
ControlToValidate="txtEmail" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="true" />
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var items = new[] {
new { FirstName = "Name1",Email="[email protected]" },
new { FirstName = "Name2",Email="[email protected]" },
new { FirstName = "Name3",Email="[email protected]" }};
grd.DataSource = items;
grd.DataBind();
}
}
我在GridView的文本框,我想驗證電子郵件ID,如果txtEmail爲空則顯示錯誤頁面別的重定向到下一個頁面.. – Rocky 2011-04-25 06:31:25
不正確的文本是否會導致RegEx驗證器驗證? – abatishchev 2011-04-25 06:39:49
你的代碼中有一個錯字:「ControlToValidate =」txtEmail />「(id後面沒有尾隨引號) – Dmitry 2011-04-25 06:43:01