简体   繁体   English

自定义验证器不会触发

[英]Custom Validator won't fire

I'm trying to force the user to choose to fill either the Photo or the Video Textbox using the CustomValidator but it's not working, I've tried searching around and from previous questions a lot of people instructed to add the ValidateEmptyText="true" property, I tried adding it but it still won't fire. 我试图强迫用户选择使用CustomValidator填充照片或视频Textbox ,但是它不起作用,我尝试搜索周围的内容,并从以前的问题中发现很多人指示添加ValidateEmptyText="true"属性,我尝试添加它,但仍然不会触发。

I'm using other RequiredFieldValidators which are operating normally. 我正在使用其他运行正常的RequiredFieldValidators

This is my aspx code of the two fields: 这是我的两个字段的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>

This is my c# Validation method : 这是我的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;
}

EDIT : This is one of the text boxes and it's validators from the output screen shots. 编辑:这是文本框之一,它是输出屏幕截图中的验证器。

    <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>

EDIT: This is the whole aspx Code: 编辑:这是整个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: 输出:

输出

This code was tested and works properly. 此代码已经过测试,可以正常工作。

<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>

with this code: 使用此代码:

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

在此处输入图片说明

if I enter any value in either of the two textboxes, the Validator is not shown. 如果我在两个文本框中的任何一个中输入任何值,则不会显示验证器。

I wanted to leave a comment but figured it would be best to display to you exactly what I tested this way you know what is working. 我想发表评论,但认为最好向您确切显示我以这种方式测试过的内容,以便您知道有效的方法。

You have to make sure the Page IsValid before creating your event... 您必须先创建Page IsValid,然后再创建活动...

protected void btn_Click(object sender, EventArgs e)
{
    if (IsValid)
    {
        Response.Write("Creating an event");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM