Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

.net - TextBox inside GridView validation

This is my code:

<asp:TemplateField HeaderText="Email">
    <ItemTemplate> 
        <asp:TextBox ID="txtEmail" runat="server" Text='<%# Eval("Email") %>' Width="88px" CausesValidation="True" />
        <asp:RegularExpressionValidator runat="server" ErrorMessage="Enter a valid email id!" ValidationExpression="w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*" ControlToValidate="txtEmail" />
        <asp:RequiredFieldValidator runat="server" ErrorMessage="*" ControlToValidate="txtEmail" />
    </ItemTemplate>
</asp:TemplateField>

when i am clicking next button required field validation is not causing validation and its not showing any error and page is re-directing on next page.

Please help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check out the following code.

enter image description here

<%@ 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="sa@gmail.com" },
                new { FirstName = "Name2",Email="test@gmail.com" },
                new { FirstName = "Name3",Email="test@gmail.com" }};

        grd.DataSource = items;

        grd.DataBind();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...