在将查询字符串作为参数与response.redirect
或windows.location.href
传递时,从重定向到另一个页面时,我的验证无效。
当我从一个页面重定向到另一个页面时:
<asp:Button ID="New" runat="server" Text="New" OnClientClick="Transfer()" />
function Transfer() {
window.location.href = "Abc.aspx?flag=yes"; //when adding query string my validation doesnt work
//window.location.href = "Abc.aspx";// When removing query string my validation successfully works
}
然后我尝试从服务器端这样:
<asp:Button ID="New" runat="server" Text="New" OnClick="New_Click" />
protected void btnNewApplicant_Click(object sender, EventArgs e)
{
Response.Redirect("Abc.aspx?flag=yes", false); //again not working with this.
}
此错误是否必须对此选项执行任何操作:EnableEventValidation="false"
,您可以在我的代码中看到?
注意:由于某种原因,我需要将参数作为查询字符串传递。
Abc.aspx :
<%@ Page Title="" Theme="---" Language="C#" MasterPageFile="---" AutoEventWireup="true" CodeBehind="---" EnableEventValidation="false" Inherits="---" %>
<asp:TextBox ID="txt1" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rf1" runat="server" ErrorMessage="require" ForeColor="Red" ControlToValidate="txt1" Display="None" ValidationGroup="validate"></asp:RequiredFieldValidator>
<asp:TextBox ID="txt2" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rf2" runat="server" ErrorMessage="require" ForeColor="Red" ControlToValidate="txt2" Display="None" ValidationGroup="validate"></asp:RequiredFieldValidator>
<asp:Button ID="btnSubmit" runat="server" Text="Save" ValidationGroup="validate" OnClick="btnSubmit_Click" UseSubmitBehavior="true" OnClientClick="checkvalidation()"/> //on click of this i want to perform validation but it is not working.
<telerik:RadCodeBlock ID="radcodeblock1" runat="server" EnableViewState="true">
<script type="text/javascript">
function checkvalidation() {
window.Page_ClientValidate('validate');
var counter= 0;
var val= '';
for (var i = 0; i < window.Page_Validators.length; i++) {
if (!window.Page_Validators[i].isvalid && typeof (window.Page_Validators[i].errormessage) == "string") {
counter= 1;
val+= '- ' + window.Page_Validators[i].errormessage + '<br>';
}
}
if (counter== 1) {
//My validation pop up to display validations alert because this counter value remains 0 so this part is not executed.
}
}
</script>
</telerik:RadCodeBlock>
现在,当我点击提交按钮时,我的服务器端代码事件被触发,但我的验证弹出窗口显示。
我甚至把这行放在web.config中:
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None"></add>
但是这仍然无法从response.redirect或windows.location.href中删除查询字符串,然后我的验证弹出窗口成功显示并且工作正常。
任何人都可以帮助我吗?
答案 0 :(得分:2)
正如您所说,window.Page_Validators[i].isvalid
是false
且typeof (window.Page_Validators[i].errormessage)
是true
,那么我们必须进入'if'条件。计数器必须设置为1,然后必须稍后进入'if'。
我已经更改了一些检查并添加了控制台日志记录来帮助您。如果有人不知道,您可以通过点击浏览器中的F12
并点击“控制台”来查看这些消息。
function checkvalidation() {
window.Page_ClientValidate('validate');
var counter= 0;
var val= '';
for (var i = 0; i < window.Page_Validators.length; i++) {
if ( (window.Page_Validators[i].isvalid === false) && typeof (window.Page_Validators[i].errormessage) == "string") {
console.log("Inside the if condition");
console.log(window.Page_Validators[i]);
counter = 1;
val+= '- ' + window.Page_Validators[i].errormessage + '<br>';
}
}
if (counter === 1) {
//My validation pop up to display validations alert because this counter value remains 0 so this part is not executed.
}
}