ASP.net验证器禁止按钮单击事件.. [英] ASP.net validators inhibit button click event..

查看:77
本文介绍了ASP.net验证器禁止按钮单击事件..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net表单,包含几个文本字段,一个下拉列表和一个注册按钮。



问题:当我应用客户端验证器时在文本字段和下拉列表中,按钮单击事件被禁止(对于页面上的所有按钮;不仅用于提交按钮,还用于从MasterPage继承的全局导航按钮)。有些字段有多个验证器(例如必填字段和reg ex)。



当我注释掉任何验证器时,所有按钮的'click事件都按预期运行。



我似乎是一个疯狂的工作,插入一个额外的不需要的''虚拟''验证器只是为了评论,但它似乎只适用于n-1验证器(即无论我在页面上有多少验证器,它都只适用于其中任何一个注释掉的)。



网上有链接到aspalliance.com上关于这个确切主题的文章但遗憾的是,链接已损坏。有人可以建议一个解决方案吗?



谢谢,Daf。



标记代码如下:





I have an asp.net form consisting of several text fields and a drop down list and a register button.

The problem: When I apply client side validators to the text fields and drop down, the button click event is inhibited (for ALL buttons on page; not just for submit button but also for global navigation buttons inheriting from a MasterPage). Some fields have more than one validator (e.g. required field and reg ex).

When I comment out ANY of the validators, all buttons'' click events function as expected.

I seems like a crazy work aroung to insert an extra unwanted ''dummy'' validator just to comment out but it only seems to work with n-1 validators (i.e. no matter how many validators I have on the page, it will only work with ANY one of them commented out).

There are links all over the web to an article about this exact topic on aspalliance.com but sadly, the link is broken. Can anybody please suggest a solution?

Thanks, Daf.

Markup code as follows:


<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/Admin.Master" AutoEventWireup="true" CodeBehind="RegisterUser.aspx.cs" Inherits="LectureQuestions.Interface.Admin.RegisterUser" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PageTitle" runat="server">Register New User
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="TitleTxt" runat="server">Register New User
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    <table ID="tblInputField"  runat="server" width="100%" cellspacing="15">
            <tr>
                <td width="10%"></td>
                <td width="20%">First Name</td>
                <td width="60%">
                    <asp:TextBox ID="txtFirstName" runat="server" Width="98%"></asp:TextBox>
                </td>
                <td width="10%">
                    <asp:RequiredFieldValidator ID="txtFirstNameRequired" runat="server" Display="Dynamic"

                        ValidationGroup="InputValidate" ControlToValidate="txtFirstName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td width="10%"></td>
                <td width="20%">Last Name</td>
                <td width="60%">
                    <asp:TextBox ID="txtLastName" runat="server" Width="98%"></asp:TextBox>
                </td>
                <td width="10%">
                    <asp:RequiredFieldValidator ID="txtLastNameRequired" runat="server" Display="Dynamic"

                        ValidationGroup="InputValidate" ControlToValidate="txtLastName" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td width="10%"></td>
                <td width="20%">E-mail</td>
                <td width="60%">
                    <asp:TextBox ID="txtEmail" runat="server" Width="98%"></asp:TextBox>
                </td>
                <td width="10%">
                    <asp:RequiredFieldValidator ID="txtEmailRequired" runat="server" Display="Dynamic"

                        ValidationGroup="InputValidate" ControlToValidate="txtEmail" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="txtEmailRx" runat="server" Display="Dynamic"

                        ValidationGroup="InputValidate" ControlToValidate="txtEmail" ErrorMessage="*" ForeColor="Red"

                        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                </td>
            </tr>
            <tr>
                <td width="10%"></td>
                <td width="20%">Phone</td>
                <td width="60%">
                    <asp:TextBox ID="txtPhone" runat="server" Width="98%"></asp:TextBox>
                </td>
                <td width="10%">
                    <asp:RequiredFieldValidator ID="txtPhoneRequired" runat="server" Display="Dynamic"

                        ValidationGroup="InputValidate" ControlToValidate="txtPhone" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                    <asp:RegularExpressionValidator ID="txtPhoneRx" runat="server" Display="Dynamic"

                        ValidationGroup="InputValidate" ControlToValidate="txtPhone" ErrorMessage="*" ForeColor="Red"

                        ValidationExpression="^[0]\d{1,2}[\s,-]\d{5,7}$"></asp:RegularExpressionValidator>
                    <!-- solve validation probs -->
                </td>
            </tr>
            <tr>
                <td width="10%"></td>
                <td width="20%">Password</td>
                <td width="60%">
                    <asp:TextBox ID="txtPassword" runat="server" Width="98%"></asp:TextBox>
                </td>
                <td width="10%">
                    <asp:RequiredFieldValidator ID="txtPasswordRequired" runat="server" Display="Dynamic"

                        ValidationGroup="InputValidate" ControlToValidate="txtPassword" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td width="10%"></td>
                <td width="20%">User Type</td>
                <td width="60%">
                    <asp:DropDownList ID="txtUserType" runat="server" Width="99.5%">
                        <asp:ListItem></asp:ListItem>
                        <asp:ListItem>Student</asp:ListItem>
                        <asp:ListItem>Lecturer</asp:ListItem>
                        <asp:ListItem>Admin</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td width="10%">
                    <asp:RequiredFieldValidator ID="txtUserTypeRequired" runat="server" Display="Dynamic"

                        ValidationGroup="InputValidate" ControlToValidate="txtUserType" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td width="10%"></td>
                <td width="20%">Course</td>
                <td width="60%">
                    <asp:DropDownList ID="txtCourse" runat="server" Width="99.5%">
                        </asp:DropDownList>
                </td>
                <td width="10%"></td>
            </tr>
        </table>

        <table ID="tblAllValidationMsg"  runat="server" width="100%" cellspacing="5">
            <tr>
                <td width="10%"></td>
                <td width="80%" align="center">
                    <asp:Label id="allValidationMsg" runat="server" height="22px" ForeColor="Red"></asp:Label>
                </td>
                <td width="10%"></td>
            </tr>
        </table>

        <table ID="tblCommandButtons"  runat="server" width="100%" cellspacing="10">
            <tr>
                <td width="25%"></td>
                <td width="25%" align="right">
                    <asp:Button class="navbutton" ID="btnRegister" runat="server" causevalidation="true"

                        ValidationGroup="InputValidate" Text="Register User" OnClick="btnRegister_Click" />
                </td>
                <td width="25%" align="left">
                    <asp:Button class="navbutton" ID="btnCancel" runat="server" causevalidation="false"

                        Text="Cancel" onclick="btnCancel_Click" />
                </td>
                <td width="25x%"></td>
            </tr>
        </table>
</asp:Content>

推荐答案

\"></asp:RegularExpressionValidator>
<!-- solve validation probs -->
</td>
</tr>
<tr>
<td width=\"10%\"></td>
<td width=\"20%\">Password</td>
<td width=\"60%\">
<asp:TextBox ID=\"txtPassword\" runat=\"server\" W idth=\"98%\"></asp:TextBox>
</td>
<td width=\"10%\">
<asp:RequiredFieldValidator ID=\"txtPasswordRequired\" runat=\"server\" Display=\"Dynamic\"

ValidationGroup=\"InputValidate\" ControlToValidate=\"txtPassword\" ErrorMessage=\"*\" ForeColor=\"Red\"></asp:Re quiredFieldValidator>
</td>
</tr>
<tr>
<td width=\"10%\"></td>
<td width=\"20%\">User Type</td>
<td width=\"60%\">
<asp:DropDownList ID=\"txtUserType\" runat=\"server\" Width=\"99.5%\">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Student</asp:ListItem>
<asp:ListItem>Lecturer </asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
</asp:DropDownList>
</td>
<td width=\"10%\">
<asp:RequiredFieldValidator ID=\"txtUserTypeRequired\" runat=\"server\" Display=\"Dynamic\"

ValidationGroup=\"InputValidate\" ControlToValidate=\"txtUserType\" ErrorMessage=\"*\" ForeColor=\"Red\"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td width=\"10%\"></td>
<td width=\"20%\">Course</td>
<td width=\"60%\">
<as p:DropDownList ID=\"txtCourse\" runat=\"server\" Width=\"99.5%\">
</asp:DropDownList>
</td>
<td width =\"10%\"></td>
</tr>
</table>

<table ID=\"tblAllValidationMsg\" runat=\"server\" width=\"100%\" cellspacing=\"5\">
<tr>
<td width=\"10%\"></td>
<td width=\"80%\" align=\"center\">
<asp:Label id=\"allValidationMsg\" runat=\"server\" height=\"22px\" ForeColor=\"Red\"></asp:Label>
</td>
<td width=\"10%\"></td>
</tr>
</table
>

<table ID=\"tblCommandButtons\" runat=\"server\" width=\"100%\" cellspacing=\"10\">
<tr>
<td width=\"25%\"></td>
<td width=\"25%\" align=\"right\">
<asp:Button class=\"navbutton\" ID=\"btnRegister\" runat=\"server\" causevalidation=\"true\"

ValidationGroup=\"InputValidate\" Text=\"Register User\" OnClick=\"bt nRegister_Click\" />
</td>
<td width=\"25%\" align=\"left\">
<asp:Button class=\"navbutton\" ID=\"btnCancel\" runat=\"server\" causevalidation=\"false\"

Text=\"Cancel\" onclick=\"btnCancel_Click\" />
</td>
<td width=\"25x%\"></td>
</tr>
</table>
</asp:Content>
"></asp:RegularExpressionValidator> <!-- solve validation probs --> </td> </tr> <tr> <td width="10%"></td> <td width="20%">Password</td> <td width="60%"> <asp:TextBox ID="txtPassword" runat="server" Width="98%"></asp:TextBox> </td> <td width="10%"> <asp:RequiredFieldValidator ID="txtPasswordRequired" runat="server" Display="Dynamic" ValidationGroup="InputValidate" ControlToValidate="txtPassword" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td width="10%"></td> <td width="20%">User Type</td> <td width="60%"> <asp:DropDownList ID="txtUserType" runat="server" Width="99.5%"> <asp:ListItem></asp:ListItem> <asp:ListItem>Student</asp:ListItem> <asp:ListItem>Lecturer</asp:ListItem> <asp:ListItem>Admin</asp:ListItem> </asp:DropDownList> </td> <td width="10%"> <asp:RequiredFieldValidator ID="txtUserTypeRequired" runat="server" Display="Dynamic" ValidationGroup="InputValidate" ControlToValidate="txtUserType" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator> </td> </tr> <tr> <td width="10%"></td> <td width="20%">Course</td> <td width="60%"> <asp:DropDownList ID="txtCourse" runat="server" Width="99.5%"> </asp:DropDownList> </td> <td width="10%"></td> </tr> </table> <table ID="tblAllValidationMsg" runat="server" width="100%" cellspacing="5"> <tr> <td width="10%"></td> <td width="80%" align="center"> <asp:Label id="allValidationMsg" runat="server" height="22px" ForeColor="Red"></asp:Label> </td> <td width="10%"></td> </tr> </table> <table ID="tblCommandButtons" runat="server" width="100%" cellspacing="10"> <tr> <td width="25%"></td> <td width="25%" align="right"> <asp:Button class="navbutton" ID="btnRegister" runat="server" causevalidation="true" ValidationGroup="InputValidate" Text="Register User" OnClick="btnRegister_Click" /> </td> <td width="25%" align="left"> <asp:Button class="navbutton" ID="btnCancel" runat="server" causevalidation="false" Text="Cancel" onclick="btnCancel_Click" /> </td> <td width="25x%"></td> </tr> </table> </asp:Content>


The solution is to work out the method that calls the validators in javascript and conditionally submits the page, and bypass it in your click events.



Or you could use the CausesValidation[^] property. Or you can use validation groups, apparently.
The solution is to work out the method that calls the validators in javascript and conditionally submits the page, and bypass it in your click events.

Or you could use the CausesValidation[^] property. Or you can use validation groups, apparently.


这篇关于ASP.net验证器禁止按钮单击事件..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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