即使验证无效,ASP.Net文本框也会触发OnTextChanged

时间:2022-05-18 03:18:08

Here's a simple version of my code:

这是我的代码的简单版本:

<asp:TextBox ID="textBox" runat="server" AutoPostBack="true" OnTextChanged="textChanged" CausesValidation="true" ValidationGroup="valGroup"></asp:TextBox>
<asp:RegularExpressionValidator ID="regEx" runat="server" ControlToValidate="textBox" ValidationGroup="valGroup" ErrorMessage="Not a valid number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSaveClick" ValidationGroup="valGroup" />

<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancelClick" CausesValidation="false" />

It's a text box with a regular expression validator that's supposed to restrict the input to be only positive real numbers. When the text changes and it passes validation a textChanged() function is called.

它是一个带有正则表达式验证器的文本框,它应该将输入限制为只有正实数。当文本更改并通过验证时,将调用textChanged()函数。

If the text is invalid, textChanged() won't fire, and the save button won't work as well.

如果文本无效,则不会触发textChanged(),并且保存按钮也不起作用。

This works as intended.

这按预期工作。

I've also got a cancel button, and it's supposed to just clear the text box (and some other things server side). It should be able post back to the server whether or not the text box is valid, hence the CausesValidation="false" tag.

我还有一个取消按钮,它应该只是清除文本框(以及服务器端的其他一些东西)。它应该能够回发到服务器,无论文本框是否有效,因此CausesValidation =“false”标记。

My problem is that when I enter something invalid into the text box, and then click cancel it actually executes textChanged() before btnCancelClick(). Even though it isn't valid.

我的问题是当我在文本框中输入无效的东西,然后单击取消它实际上在btnCancelClick()之前执行textChanged()。即使它无效。

This is a huge problem because textChanged() uses the text box text as a number, so the page throws an exception.

这是一个很大的问题,因为textChanged()使用文本框文本作为数字,因此页面会抛出异常。

I have no idea what's causing textChanged() to be called. For one thing, the cancel button doesn't even call that function. And the regular expression validator should prevent the text box from calling it.

我不知道是什么导致调用textChanged()。首先,取消按钮甚至不会调用该功能。并且正则表达式验证器应该阻止文本框调用它。

Edit: I've tried making some changes and experimenting.

编辑:我已尝试进行一些更改和实验。

function cancel()
{

  document.getElementById('<%=textBox.ClientID%>').value = "";

}

<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="cancel()" CausesValidation="false" />

I switched the cancel button to call a JavaScript method to try to clear the text box. The results are kind of interesting.

我切换了取消按钮来调用JavaScript方法来尝试清除文本框。结果很有意思。

When an invalid string is entered as the first thing, then the cancel button works fine. It clears the text box without textChanged() being called.

当输入无效字符串作为第一件事时,取消按钮工作正常。它会在没有调用textChanged()的情况下清除文本框。

If I type in a valid number, textChanged() is called as I expect. But when I click the cancel button it does call cancel(), and immediately after that it calls textChanged() again, but now the text box is empty, leading to an exception.

如果我输入有效数字,则按照我的预期调用textChanged()。但是当我单击取消按钮时,它会调用cancel(),然后立即再次调用textChanged(),但现在文本框为空,导致异常。

A similar thing happens if I enter something valid, and then make it invalid. textChanged() is called for the valid input. When the incorrect input is added, the validator prevents textChanged() from being called. But when I click cancel it goes from cancel() to textChanged().

如果我输入有效的东西,然后使其无效,就会发生类似的事情。为有效输入调用textChanged()。添加错误输入时,验证程序会阻止调用textChanged()。但是当我点击取消时,它会从cancel()转到textChanged()。

I was thinking that maybe it was because validators don't persist through postback, but with the JavaScript function cancel() the button can't cause the page to postback. textChanged() has to be what's causing it, but I have no idea how.

我想也许是因为验证器不会通过回发持续存在,但是使用JavaScript函数cancel()按钮不会导致页面回发。 textChanged()必须是导致它的原因,但我不知道如何。

Edit 2: I'm not going to mark this solved, since it's still a huge problem in my opinion, but I have found a workaround.

编辑2:我不打算将此标记解决,因为在我看来它仍然是一个很大的问题,但我找到了一个解决方法。

<script>

    function cancel() {

        document.getElementById('<%=textBox.ClientID%>').value = "";
        return false;

    }

</script>

<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClientClick="return cancel();" CausesValidation="false" />

This doesn't allow the button to cause a postback under any circumstances. It's fixing something that shouldn't be there in the first place, but there it is.

这不允许按钮在任何情况下都会引发回发。它正在修复一些不应该存在的东西,但它确实存在。

This helps with the button click, but not with other things. I've got a GridView with an OnSelectedIndexChanged function server-side. It also causes the page to post back with the textChanged() function. I could try to create a JavaScript version of the GridView function and fix it like the cancel button, but at this stage I'll probably remove the OnTextChanged event handler and put the textChanged() function and put it behind a button.

这有助于点击按钮,但不能与其他东西一起使用。我有一个带有OnSelectedIndexChanged函数服务器端的GridView。它还会使页面使用textChanged()函数进行回发。我可以尝试创建一个JavaScript版本的GridView函数并像取消按钮一样修复它,但在这个阶段我可能会删除OnTextChanged事件处理程序并将textChanged()函数放在一个按钮后面。

2 个解决方案

#1


0  

add causesvalidation="true" works for me on the textbox control.

添加causevalidation =“true”对我的文本框控件有效。

#2


0  

So close... What's needed is simply setting a dedicated name to the ValidationGroup name on the Cancel button. This will cause it to be processed out of cycle with the rest of the validation. Not setting it effectively puts it in the "default" validation group which behaves (as you've seen), a bit oddly.

如此接近......只需在“取消”按钮上为ValidationGroup名称设置专用名称即可。这将导致在剩余的验证过程中将其处理为循环。没有设置它有效地把它放在“默认”验证组中,行为(如你所见),有点奇怪。

To allow the Cancel button to be exempt from the rest of the form validation, the rule I generally apply is [ValidationGroupName]Cancel to ensure it's triggered outside that block of validation. In this case, setting the Cancel button to something like;

为了允许取消按钮免于表单验证的其余部分,我通常应用的规则是[ValidationGroupName]取消,以确保它在该验证块之外被触发。在这种情况下,将取消按钮设置为类似;

<asp:Button ID="btnCancel" runat="server" Text="Cancel" ValidationGroup="valGroupCancel" OnClick="btnCancelClick" />

Should give you your expected behaviour.

应该给你预期的行为。

#1


0  

add causesvalidation="true" works for me on the textbox control.

添加causevalidation =“true”对我的文本框控件有效。

#2


0  

So close... What's needed is simply setting a dedicated name to the ValidationGroup name on the Cancel button. This will cause it to be processed out of cycle with the rest of the validation. Not setting it effectively puts it in the "default" validation group which behaves (as you've seen), a bit oddly.

如此接近......只需在“取消”按钮上为ValidationGroup名称设置专用名称即可。这将导致在剩余的验证过程中将其处理为循环。没有设置它有效地把它放在“默认”验证组中,行为(如你所见),有点奇怪。

To allow the Cancel button to be exempt from the rest of the form validation, the rule I generally apply is [ValidationGroupName]Cancel to ensure it's triggered outside that block of validation. In this case, setting the Cancel button to something like;

为了允许取消按钮免于表单验证的其余部分,我通常应用的规则是[ValidationGroupName]取消,以确保它在该验证块之外被触发。在这种情况下,将取消按钮设置为类似;

<asp:Button ID="btnCancel" runat="server" Text="Cancel" ValidationGroup="valGroupCancel" OnClick="btnCancelClick" />

Should give you your expected behaviour.

应该给你预期的行为。