none
RequiredFieldValidator 控件判断非空,加了个弹出窗口,就出问题了! RRS feed

  • 问题

  •    我用RequiredFieldValidator来判断和它对应的控件TextBox_LeaseDtaeFrom非空,加上了这么一段script,让它可以弹出一个提示窗口,可是每次一打开页面,出现TextBox_LeaseDtaeFrom为空的错误提示,我只想RequiredFieldValidator在我按“确定输入”时才判断,而不是以开始就判断,该怎么办呢?

    <asp:RequiredFieldValidator ID="RequiredFieldValidator_LeaseDateFrom" 
                        runat="server" ControlToValidate="TextBox_LeaseDtaeFrom" ErrorMessage="“合同起始日”不能为空!" 
                        Font-Size="Small"  Display="Dynamic"
                        Text="<script type='text/javascript'>alert('*输入字段不能为空!')</script>"></asp:RequiredFieldValidator>
    
    
    <asp:TextBox ID="TextBox_LeaseDtaeFrom" runat="server" 
                        CssClass="TextBox1"></asp:TextBox>


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月14日 9:03

答案

  • 我忘记把alert删除了,完整代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CSharp.WebForm1" %>
     
    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function MyFunc(sender, args) {
                args.IsValid = (args.Value != "No Choice");
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>No Choice</asp:ListItem>
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
            </asp:DropDownList>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="MyFunc" ControlToValidate="DropDownList1" Display="Dynamic" ErrorMessage='你不能选择"No Choices"!' OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        
        </div>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="SingleParagraph" ShowMessageBox="True" ShowSummary="False" />
        </form>
    </body>
    </html>

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月17日 8:29

全部回复

  • 1)把对Text赋值的内容全部去除,给ErrorMessage写入要弹出对话框的内容。

    2)拖拽一个ValidationSummary到WebForm上。

    3)代码:

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage="必须填写!">*</asp:RequiredFieldValidator>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="SingleParagraph" ShowMessageBox="True" ShowSummary="False" />


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月16日 4:06
  •     感谢您的回复,第一次使用ValidationSummary这个控件,而且很好用!

        它可以用来和RequiredFieldValidator配合使用,和CustomValidator似乎就不能配合使用了。我的代码如下图:RequiredFieldValidator不会弹出错误提示的对话框。我哪里弄错了吗?

    --------------------------前台---------------------------

    <asp:DropDownList ID="DropDownList_ContractType" runat="server" 
                        DataSourceID="SqlDataSource_ContractType" DataTextField="ContractType" 
                        DataValueField="ContractType"  AutoPostBack="True" 
                        onselectedindexchanged="DropDownList_ContractType_SelectedIndexChanged" 
                            CssClass="dropdown1">
                    </asp:DropDownList>
    
    <asp:CustomValidator ID="CustomValidator_ContractType" runat="server" 
                        ControlToValidate="DropDownList_ContractType" Font-Size="Small" 
                        Display="None"  ErrorMessage="合同类别未选择"
                        onservervalidate="CustomValidator_ContractType_ServerValidate"></asp:CustomValidator>
    
    <asp:SqlDataSource ID="SqlDataSource_ContractType" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" 
                        
                        SelectCommand="SELECT N'未选择' AS [ContractType] UNION ALL SELECT DISTINCT [ContractType] FROM [ContractTable]">
                    </asp:SqlDataSource>

    -----------------------后台-------------------------

    protected void CustomValidator_ContractType_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (DropDownList_ContractType.SelectedItem.Value == "未选择")
            {
                args.IsValid = false;
                DropDownList_ContractType.Focus();
                DropDownList_ContractType.BorderColor = Color.Red;
    
            }
            else
            { 
                args.IsValid = true;
                DropDownList_ContractType.BorderColor = ColorTranslator.FromHtml("#C0C0C0");
            }
    
        }


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!



    2013年6月17日 3:14
  • CustomerValidation主要用于服务端,它的客户端你自己需要指定:

      <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>No Choice</asp:ListItem>
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
            </asp:DropDownList>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="MyFunc" ControlToValidate="DropDownList1" Display="Dynamic" ErrorMessage="你必须选择!" OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

    然后增加一个js验证:

     <script type="text/javascript">
            function MyFunc(sender, args) {
                args.IsValid = (args.Value != "No Choice");
                alert("你不能选择\"No Choices!\"");
            }
        </script>

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月17日 4:23
  •     谢谢您的解答,可是我测试了您提供的例子,全部都提示No Choise

    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月17日 8:21
  • 我忘记把alert删除了,完整代码:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CSharp.WebForm1" %>
     
    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function MyFunc(sender, args) {
                args.IsValid = (args.Value != "No Choice");
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
            <asp:DropDownList ID="DropDownList1" runat="server">
                <asp:ListItem>No Choice</asp:ListItem>
                <asp:ListItem>1</asp:ListItem>
                <asp:ListItem>2</asp:ListItem>
            </asp:DropDownList>
            <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="MyFunc" ControlToValidate="DropDownList1" Display="Dynamic" ErrorMessage='你不能选择"No Choices"!' OnServerValidate="CustomValidator1_ServerValidate">*</asp:CustomValidator>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        
        </div>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="SingleParagraph" ShowMessageBox="True" ShowSummary="False" />
        </form>
    </body>
    </html>

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年6月17日 8:29
  •     谢谢您的解答,会做了。但用Script的话,我不会给控件的边框添加彩色,我只会用后台的代码来添加!
    else
            {
                args.IsValid = true;
                DropDownList_ContractType.BorderColor = ColorTranslator.FromHtml("#C0C0C0");
            }


    C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!

    2013年6月18日 0:42