ASP.NET CasusesValidation="false" not working on link button in IE
I have an issue with causes validation on my link button in my aspx page. What I have is a page with two text boxes, some validation controls and a submit button. The validation controls have enable client script set to true, and have validation groups. My asp image button has a validation group assigned and causes validation equal to true. I have the focus set to my first text box.
My link button is a click here to learn more about..... and I've specified the validation group and set causes validation to false. In Chrome, everything works fine. In IE, when either clicking any where on the page besides the submit button, or when clicking my link button, my first text box is validated and the error is presented. Now, if I click on the link button again, I am redirected to the page I desire.
This seems a bit weird to me, any one have any ideas?
So, how do I fix this, so IE won't validate until only the submit button is clicked.
Here is what 开发者_运维技巧my code looks like:
<span>Value 1:</span>
<asp:TextBox ID="Value1TextBox" runat="server" />
<asp:RequiredFieldValidator ID="Value1_1_Validator" EnableClientScript="true" ControlToValidate="Value1TextBox" Display="Dynamic" ForeColor="Red" Text="is required" ValidationGroup="MyValGroup" runat="server" />
<asp:RegularExpressionValidator ID="Value1_2_Validator" EnableClientScript="true" ControlToValidate="Value1TextBox" ValidationExpression="\somevalexpression\" Display="Dynamic" ForeColor="Red" Text="is not valid" ValidationGroup="MyValGroup" runat="server" />
...
<span>Value 2:</span>
<asp:TextBox ID="Value2TextBox" runat="server" />
<asp:RequiredFieldValidator ID="Value2_1_Validator" EnableClientScript="true" ControlToValidate="Value2TextBox" Display="Dynamic" ForeColor="Red" Text="is required" ValidationGroup="MyValGroup" runat="server" />
...
...
<asp:ImageButton ID="SubmitButton" ImageUrl="~/Images/submit.png" OnClick="SubmitButton_Click" ValidationGroup="MyValGroup" CausesValidation="true" runat="server" />
...
<asp:LinkButton ID="OurPolicyLink" PostBackUrl="~/Policy.aspx" ValidationGroup="MyValGroup" CausesValidation="false" Text="click here" runat="server" />
I came up with a fix using jquery and javascript, its not as clean, but it does work. I would have thought just doing causes validation equal to false would be enough. Perhaps I was wrong.
I have a script javascript file, here is what is looks like.
$(function () {
$"[id$=_Value1TextBox]").focus();
$("[id$=_Validator]").each(function () {
$(this)[0].enabled = false;
});
$("[id$=_SubmitButton]").click(function () {
$("[id$=_Validator]").each(function () {
$(this)[0].enabled = true;
});
Page_ClientValidate("MyValGroup");
if (!Page_IsValid) {
return false;
}
return true;
});
});
It is because you are setting the focus on the text box. When the focus leaves the text box it is triggering validation. Either don't set the focus on the text box or set CuasesValidation=False on the TextBox. The problem with latter option is that it won't validate the text box until something else in the same validation group triggers the validation.
精彩评论