自定义验证器不会触发

时间:2018-12-07 20:39:49

标签: c# asp.net

我正在试图迫使用户选择使用Textbox来填充照片或视频CustomValidator,但是它不起作用,我尝试了很多搜索,并从以前的问题中进行了搜索有人指示添加ValidateEmptyText="true"属性,我尝试添加它,但仍然不会触发。

我使用的其他RequiredFieldValidators正常运行。

这是我的aspx这两个字段的代码:

<asp:Button ID="btn1" runat="server" Text="+"/>
<asp:TextBox runat="server" PlaceHolder="Photos" ID="pics" ValidationGroup="txt1"></asp:TextBox>
<br />
<asp:Button ID="btn2" runat="server" Text="+"/>
<asp:TextBox ID="vids" runat="server" PlaceHolder="Videos" ValidationGroup="txt1"></asp:TextBox>
<asp:CustomValidator runat="server" ErrorMessage="Please enter either a photo or a picture!" OnServerValidate="ValidateBoxes" ValidationGroup="txt1" ValidateEmptyText="true"></asp:CustomValidator>

这是我的c# Validation method

public void ValidateBoxes(object sender, ServerValidateEventArgs e)
{
    if (string.IsNullOrEmpty(pics.Text) && string.IsNullOrWhiteSpace(vids.Text))
        e.IsValid = false;
    else
        e.IsValid = true;
}

编辑::这是文本框之一,它是输出屏幕截图中的验证器。

    <asp:TextBox ID ="city_in" PlaceHolder ="Enter city" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="city_in" ErrorMessage="Please enter the city!" ForeColor="Red"></asp:RequiredFieldValidator>

编辑:这是整个aspx代码:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h1>
    Creating An Event
</h1>
    <br />
<h3>
    Please Provide the information below
</h3>
<asp:TextBox ID ="city_in" PlaceHolder ="Enter city" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="city_in" ErrorMessage="Please enter the city!" ForeColor="Red"></asp:RequiredFieldValidator>
    <br />
    <br />
<asp:TextBox ID="date" runat="server" PlaceHolder ="Enter date" TextMode="Date" ></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="date" ErrorMessage="Please enter the date!" ForeColor="Red" ></asp:RequiredFieldValidator>
    <br />
    <br />
<asp:TextBox ID="desc" runat="server" PlaceHolder = "Description"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="desc" ErrorMessage="Please enter the description!" ForeColor="Red"></asp:RequiredFieldValidator>
    <br />
    <br />
<asp:TextBox ID ="entertain" runat="server" PlaceHolder ="Entertainer"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ControlToValidate="entertain" ErrorMessage="Please enter the entertainer!" ForeColor="Red"></asp:RequiredFieldValidator>
    <br />
    <br />
<asp:TextBox ID ="viewer" runat="server" PlaceHolder ="ID"></asp:TextBox>
    <br />
    <br />
<asp:TextBox ID ="location" runat="server" PlaceHolder ="Location"></asp:TextBox>
<asp:RequiredFieldValidator runat="server" ErrorMessage="Please enter the location!" ControlToValidate="location" ForeColor="Red"></asp:RequiredFieldValidator>
<br />
<br />
<p>
    Please choose what type of Multimedia you would like to upload
</p>
<br />
<asp:Button ID="btn1" runat="server" Text="+"/>
<asp:TextBox runat="server" PlaceHolder="Photos" ID="pics" ></asp:TextBox>
<br />
<asp:Button ID="btn2" runat="server" Text="+"/>
<asp:TextBox ID="vids" runat="server" PlaceHolder="Videos"></asp:TextBox>
<asp:CustomValidator runat="server" ErrorMessage="Please enter either a photo or a picture!" OnServerValidate="ValidateBoxes" ValidateEmptyText="true"></asp:CustomValidator>
<br />
<br />
<asp:Button ID ="btn" runat="server" Text="Create Event" OnClick="create_Event" />
<asp:Button runat="server" Text="Cancel" OnClick="go_Profile"/>

输出:

output

2 个答案:

答案 0 :(得分:0)

此代码已经过测试,可以正常工作。

<body>
    <form id="form1" runat="server">
        <p>
            Please choose what type of Multimedia you would like to upload
        </p>
        <br />

        <asp:TextBox runat="server" PlaceHolder="Photos" ID="pics"></asp:TextBox>
        <br />

        <asp:TextBox ID="vids" runat="server" PlaceHolder="Videos"></asp:TextBox>
        <asp:CustomValidator runat="server" ErrorMessage="Please enter either a photo or a picture!" OnServerValidate="ValidateBoxes" ValidateEmptyText="true"></asp:CustomValidator>
        <br />
        <br />
        <asp:Button ID="btn" runat="server" Text="Create Event"  />
        <asp:Button runat="server" Text="Cancel" />
    </form>
</body>

使用以下代码:

public void ValidateBoxes(object sender, ServerValidateEventArgs e)
        {
            if (string.IsNullOrEmpty(pics.Text) && string.IsNullOrWhiteSpace(vids.Text))
                e.IsValid = false;
            else
                e.IsValid = true;
        }

enter image description here

如果我在两个文本框中的任何一个中输入任何值,则不会显示验证器。

我想发表评论,但认为最好向您准确显示我以这种方式测试过的内容,以便您知道有效的方法。

答案 1 :(得分:0)

创建活动之前,您必须确保Page IsValid ...

LowerCaseFilter