2

My validations are not working when redirecting from one page to another on passing query string as a parameter with response.redirect or with windows.location.href.

When I am redirecting from one page to another page with this:

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

Then I have tried from server side like this:

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

When I click on this New button i am getting error in console:

enter image description here

Is this error has to do anything with this option: EnableEventValidation="false" as you can see in my code?

Note: I need to pass parameter as query string for some reason.

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>

Now when I click on submit button then my server side code event is fired but my validation pop up doesn't appear.

I have even put this line in web.config:

  <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"></add>

But this is still not working as removing query string from response.redirect or from windows.location.href then my validation pop up successfully appears and it is working fine.

13
  • try returning false from the button client-side click when the page is not valid. Ensure you have no errors before that and during that code execution. Commented Nov 17, 2015 at 15:39
  • @rdmptn:but problem is my validation pop up are not firing because it is not going in my if condition Commented Nov 20, 2015 at 8:58
  • 1
    Your 'if' condition - are both of those statements false or just one? Commented Nov 20, 2015 at 9:05
  • 1
    Then only logical thing to do is to log this: window.Page_Validators and then check if it is even array and what type of error it contains. Commented Nov 20, 2015 at 9:05
  • 1
    Is 'window.Page_Validators[i].isvalid' true or false? Is 'typeof (window.Page_Validators[i].errormessage) == "string")' true or false? Commented Nov 20, 2015 at 13:20

1 Answer 1

2
+100

If, as you say, window.Page_Validators[i].isvalid is false and typeof (window.Page_Validators[i].errormessage) is true, then we must go into the 'if' condition. The counter must be set to 1, and then must go into the 'if' later on.

I've changed the checks a little bit and have added the console logging to help you. In case anyone doesn't know, you can view these messages by hitting F12 in the browser and clicking "Console".

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.
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.