ASP.NET自定义验证和AJAX [英] ASP.NET Custom Validator and AJAX

查看:171
本文介绍了ASP.NET自定义验证和AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个自定义验证与AJAX,它工作正常:

I implemented a Custom Validator with AJAX, and It works fine:

EDITED 。添加ASPX code

EDITED. Add ASPX code

<asp:DetailsView ID="DetailsView" runat="server" Height="50px" Width="25em" DataSourceID="SqlDataSource1" AutoGenerateRows="False" DefaultMode="Insert" CellPadding="4" ForeColor="#333333" GridLines="None" OnItemInserting="DetailsView_ItemInserting">
        <Fields>
            <asp:TemplateField HeaderText="Nombre *">
                <InsertItemTemplate>
                    <asp:TextBox ID="txtNombre" runat="server" Text='<%# Bind("nombre) %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="requireNombre" runat="server" ControlToValidate="txtNombre" ErrorMessage="El campo 'Nombre' no puede estar vacío." ValidationGroup="DetailsGroup" Display="None"></asp:RequiredFieldValidator>
                    <asp:CustomValidator id="CustomValidator1" ControlToValidate="txtNombre" ClientValidationFunction="validateNombre" ValidationGroup="DetailsGroup" Display="none" ErrorMessage="Introduzca un nombre diferente." runat="server"/>
                </InsertItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView>

功能AJAX:

Function AJAX:

        function validateNombre(src, args) {
        var isValid;
        $.ajax({
            type: "POST",
            url: "Nombre.aspx/ComprobarNombre",
            data: "{'nombre': '" + args.Value + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (msg) {
                isValid = msg.d;
            }
        });
        args.IsValid = isValid;

ComprobarNombre: [WebMethod的()]

ComprobarNombre: [WebMethod()]

    public static bool ComprobarNombre(string nombre)
        {
            /* SQLConnections... */

            if(...)
                 return false;
            else
                 return true;
        }

现在的问题是,当我每次在文本框中输入一个值的AJAX功能处于启动状态。我需要的功能推出,只有当我点击窗体的按钮。

The problem is that the AJAX function is launched when every time I enter a value in the TextBox. I need that the function launch only when I click the button of the form.

推荐答案

1)消除自定义验证 2)单击按钮调用客户端功能validateNombre() 3)获取函数内部的文本数据 4),并把它作为数据参数

1) Eliminate Custom validator 2) On button click call client side function validateNombre() 3) Fetch the textbox data inside the function 4) and pass it in as data parameter

这篇关于ASP.NET自定义验证和AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆