【问题标题】:TextBox inside GridView validationGridView 验证中的 TextBox
【发布时间】:2011-08-12 03:54:33
【问题描述】:

这是我的代码:

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

当我单击下一步按钮时,必填字段验证不会导致验证,并且它没有显示任何错误并且页面正在重定向到下一页。

请帮帮我。

【问题讨论】:

  • 我在 gridview 中有文本框,如果 txtEmail 为空白,我想验证电子邮件 ID,然后显示错误,否则页面重定向到下一页..
  • 不正确的文本是否会导致 RegEx 验证器验证?
  • 您的代码中有错字:“ControlToValidate="txtEmail />"。(id 后没有尾随引号)
  • 发布完整代码。 aspx 和后面的代码。我们将竭诚为您提供更多帮助...

标签: .net asp.net regex validation gridview


【解决方案1】:

查看以下代码。

<%@ 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();
    }
}

【讨论】:

    【解决方案2】:

    验证组可能有问题,您可以检查一下。

    其次,您需要在按钮控件中设置CausesValidation="true"

    另外,您的电子邮件正则表达式也不正确。

    \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* // yours
    \w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* // correct one
    

    【讨论】:

    • 这 2 个正则表达式之间的唯一区别是它允许在 @ 之前的部分使用撇号
    • 但正则表达式导致错误,当我单击转到下一页按钮时,仅需要字段不会导致错误..
    • @Ansari;尝试改变表达方式。
    • 我更改了表达式,但必填字段尚未生效
    • 你的按钮有验证组属性吗?
    【解决方案3】:

    您是否将Script Manager 添加到您的项目中?

    如果是这样,您的验证码没有问题。

    只是一个想法,也许你应该添加到验证控件EnableClientScript="false"

    【讨论】:

    • 我没有使用任何ajax工具,所以不需要添加脚本管理器
    • EnableClientScript="false" 后要求字段不起作用
    【解决方案4】:

    您需要检查您点击的下一步按钮是否启用了CausesValidation

    此外,请尝试为必填字段验证器设置 InitialValue-""

    编辑

    尝试修改代码以添加客户端脚本,例如

    button.Attributes.Add("onclick","return methodname");
    

    【讨论】:

    • ya 它的启用但需要字段不会导致验证。
    • 我的 btnnext 没有引起验证 :( :( 我只卡在这个中
    • 我相信是 ClientClick 引起的问题,您可以通过后面代码中的脚本块注册它
    • 当我通过键盘的选项卡 btn 将一个文件移动到另一个文件时,我的验证正在工作,但是当点击 btnnext 时验证不起作用
    【解决方案5】:
    <script type="text/javascript" language="javascript">
    function ValidateText(i) 
    {
        if(i.value.length>0) 
        {
        i.value = i.value.replace(/[^\d]+/g, ''); 
        }
    }
    
    </script>
    
    <asp:TemplateField >
          <ItemTemplate>
                  <asp:TextBox ID="TextBox1" runat="server" onkeyup ="ValidateText(this);"></asp:TextBox>
         </ItemTemplate>
    
    </asp:TemplateField>  
    
    <    /asp:TemplateField>  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 2010-11-14
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      相关资源
      最近更新 更多