ASP.NET验证错误消息以更改标签文本 [英] Asp.net validation error message to change labels text

查看:57
本文介绍了ASP.NET验证错误消息以更改标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用asp.net验证控件来验证用户输入.我想要的是使用验证控件生成的错误消息来更改标签文本.

I am using asp.net validation controls for validating user input. What i want is to change the label text with the error message generated by the validation control.

<asp:Label ID="Label1" runat="server" Text="Whats your name"></asp:Label>
<asp:TextBox ID="nameB" Width="322px" Height="30px" runat="server"></asp:TextBox>

<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup="business" runat="server" ErrorMessage="Please tell us your name." ControlToValidate="nameBuisness" CssClass="errMsg" Display="Dynamic"></asp:RequiredFieldValidator>

谢谢.

推荐答案

一种方法是处理提交按钮的 OnClientClick -事件并调用javascript函数,例如:

One way is to handle the submit-button's OnClientClick-event and call a javascript function like:

<script type="text/javascript">
    function displayValidationResult() {
        // Do nothing if client validation is not available
        if (typeof (Page_Validators) == "undefined") return;
        var LblName = document.getElementById('LblName');
        var RequiredName = document.getElementById('RequiredName');
        ValidatorValidate(RequiredName);
        if (!RequiredName.isvalid) {
            LblName.innerText = RequiredName.errormessage;
        }
    }
</script>

<asp:Label ID="LblName" runat="server" Text="Whats your name"></asp:Label>
<asp:TextBox ID="TxtNameBusiness" Width="322px" Height="30px" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredName" ValidationGroup="business"
    runat="server" ErrorMessage="Please tell us your name." ControlToValidate="TxtNameBusiness"
    CssClass="errMsg" Display="None"></asp:RequiredFieldValidator>
<asp:Button ID="BtnSumbit" runat="server"  Text="Submit"
    OnClientClick="displayValidationResult()" ValidationGroup="business" />

我使用了一些可用的ASP.NET客户端验证方法.我还将您的控件重命名为更有意义,并添加了一个提交按钮.

I've used some of the few ASP.NET client validation methods available. I've also renamed your controls to somewhat more meaningful and added a submit-button.

ASP.NET深度验证

这篇关于ASP.NET验证错误消息以更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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