如何在警报框中显示验证控件错误消息?

时间:2011-02-01 12:30:17

标签: asp.net

我正在使用4个必填字段验证器,4个正则表达式验证器和4个文本框的4个比较验证器。是否可以显示错误消息

验证失败时在警告或消息框中

如果可能,请发送代码示例。

此致

NSJ

5 个答案:

答案 0 :(得分:13)

    <form id="form1" runat="server">
    <asp:Label ID="lblNameRequired" runat="server" Text="*Name :"></asp:Label>
    <asp:TextBox ID="txtNameRequired" runat="server" ValidationGroup="Validation"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidatorName" runat="server" ControlToValidate="txtNameRequired"
        Display="None" ErrorMessage="Name is Required" ValidationGroup="Validation"></asp:RequiredFieldValidator>
    <br />
    <asp:Label ID="lblGenderRequired" runat="server" Text="*Gender :"></asp:Label>
    <asp:DropDownList ID="ddlGenderRequired" runat="server" ValidationGroup="Validation">
        <asp:ListItem Selected="True" Value="-1">--Select--</asp:ListItem>
        <asp:ListItem Value="0">Male</asp:ListItem>
        <asp:ListItem Value="1">Female</asp:ListItem>
    </asp:DropDownList>
    <asp:CompareValidator ID="CompareValidatorGender" runat="server" ControlToValidate="ddlGenderRequired"
        Display="None" ErrorMessage="Gender is Required" Operator="NotEqual" ValidationGroup="Validation"
        ValueToCompare="-1"></asp:CompareValidator>
    <br />
    <asp:Label ID="lblValidation" runat="server" Text="Fields marked with * are required"></asp:Label>
    <br />
    <asp:Button ID="btnValidate" runat="server" Text="Validate Input" ValidationGroup="Validation" />
    <br />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
        ShowSummary="False" ValidationGroup="Validation" />
    </form>

您应该在所有验证控件上使用相同的ValidationGroup文本,并使用ValidationGroup和ShowMessageBox =“true”添加ValidationSummary

答案 1 :(得分:3)

使用以下代码,只需设置您想要的正确消息:

<asp:ValidationSummary ID="ValidationSummary1"   ShowMessageBox ="true" 
                    runat="server"  />  


<asp:textbox id="txt1" runat="server"></asp:textbox>
&nbsp;<asp:regularexpressionvalidator id="RegularExpressionValidator1" runat="server"
    controltovalidate="txt1" errormessage="Please Enter Only Numbers" validationexpression="^[-+]?\d+(\.\d+)?$">
    </asp:regularexpressionvalidator>
&nbsp;
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt1"
    errormessage="please enter txt1">*</asp:requiredfieldvalidator>
<asp:textbox id="txt2" runat="server"></asp:textbox>
&nbsp;<asp:regularexpressionvalidator id="RegularExpressionValidator2" runat="server"
    controltovalidate="txt2" errormessage="Please Enter Only Charcters" validationexpression="^[a-zA-Z\s.]*$">
    </asp:regularexpressionvalidator>
&nbsp;
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt2"
    errormessage="please enter txt2">*</asp:requiredfieldvalidator>
    &nbsp;
<asp:textbox id="txt3" runat="server"></asp:textbox>
<asp:requiredfieldvalidator id="RequiredFieldValidator1" runat="server" controltovalidate="txt3"
    errormessage="please enter txt3">*</asp:requiredfieldvalidator>





<asp:Button ID="btnok" runat="server" Text="ok"/>

答案 2 :(得分:1)

Don't do it. Users hate alert box error messages. It is a horrible UI design. Put the error messages on the form where the user can read them and they stay while the user makes changes or moves on without having to click on an alert box. Especially when there are multiple corrections to be made, they need to be able to read the errors while they make the corrections.

答案 3 :(得分:0)

您可以使用此功能 将参数验证消息和控制ID设置为参数

protected void PopupMessage(string Msg, Control controlID)
    {
       ScriptManager.RegisterClientScriptBlock(controlID, controlID.GetType(), "msg", "alert('" + Msg + "');", true);

    }

在按钮点击事件中,您可以根据您的逻辑进行此功能

protected void btnok_Click(object sender, EventArgs e)
    {
        if(TextBox1.Text=="")
            PopupMessage("Name is Required", btnok);
    }

答案 4 :(得分:0)

请遵循以下代码,这对我来说很有用,可以使用ShowSummary属性来减少邮件重复进入您的面板

<asp:ValidationSummary ID="VS1" ShowMessageBox="true" runat="server" ShowSummary="False" />

<asp:Label ID="lblUsername" runat="server" >User name</asp:Label>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFV1" runat="server" ControlToValidate="txtUserName" ErrorMessage="Please Select Name" Display="None" SetFocusOnError="True">
</asp:RequiredFieldValidator><br />